我正在创建一个基于某些数据库数据的多个框的APP。当然,这些框需要以特定格式显示特定信息。
以编程方式,我在过去几天尝试了RelativeLayout
,LinearLayout
,GridLayout
,甚至TableLayout
并且无法获得正确的结果
这是我到目前为止代码的基本部分......
// Create Main Box for spacing
RelativeLayout acctrl = new RelativeLayout(this);
RelativeLayout.LayoutParams acctrlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 225);
acctrlp.setMargins(15, 5, 15, 5);
acctrl.setBackgroundResource(R.drawable.main_account_box);
acctrl.setId(R.id.rlAcctId);
acctrl.setClickable(true);
acctrl.setOnClickListener(acctclick);
acctrl.setOnLongClickListener(acctlongclick);
// Account New Ball & Count
RelativeLayout rlNew = new RelativeLayout(this);
RelativeLayout.LayoutParams rlpNew = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
rlpNew.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlpNew.addRule(RelativeLayout.CENTER_IN_PARENT);
rlNew.setId(R.id.rlNewId);
ImageView ivNew = new ImageView(this);
RelativeLayout.LayoutParams ivpNew = new RelativeLayout.LayoutParams(168,168);
ivpNew.addRule(RelativeLayout.CENTER_HORIZONTAL);
ivpNew.addRule(RelativeLayout.CENTER_IN_PARENT);
ivNew.setBackgroundResource(R.drawable.acct_msg_ball_blue);
rlNew.addView(ivNew,ivpNew);
TextView tvNewCount = new TextView(this);
RelativeLayout.LayoutParams tvpNewCount = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tvpNewCount.addRule(RelativeLayout.CENTER_HORIZONTAL);
tvpNewCount.addRule(RelativeLayout.CENTER_VERTICAL);
tvNewCount.setId(R.id.tvNewCountId);
tvNewCount.setTextColor(COLOR.Black);
tvNewCount.setPadding(0, -5, 0, 0);
tvNewCount.setTextSize(25);
tvNewCount.setText(String.valueOf(acct.get_new()));
rlNew.addView(tvNewCount,tvpNewCount);
TextView tvNewTitle = new TextView(this);
RelativeLayout.LayoutParams tvpNewTitle = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tvpNewTitle.addRule(RelativeLayout.CENTER_HORIZONTAL);
tvpNewTitle.addRule(RelativeLayout.BELOW,R.id.tvNewCountId);
tvNewTitle.setTextColor(COLOR.Black);
tvNewTitle.setTextSize(10);
tvNewTitle.setPadding(0, 5, 0, 0);
tvNewTitle.setText("new");
rlNew.addView(tvNewTitle,tvpNewTitle);
acctrl.addView(rlNew);
// Account Name
TextView tvName = new TextView(this);
RelativeLayout.LayoutParams tvpName = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tvpName.addRule(RelativeLayout.ALIGN_PARENT_TOP);
tvpName.addRule(RelativeLayout.RIGHT_OF,R.id.rlNewId);
tvpName.addRule(RelativeLayout.LEFT_OF,R.id.tvCountId);
tvName.setId(R.id.tvNameId);
tvName.setTextColor(COLOR.Black);
tvName.setPadding(5, 0, 5, 5);
tvName.setTextSize(25);
tvName.setText(acct.get_name());
tvName.setEllipsize(TextUtils.TruncateAt.END);
tvName.setSingleLine();
acctrl.addView(tvName, tvpName);
// Account Position
TextView tvPos = new TextView(this);
RelativeLayout.LayoutParams tvpPos = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
tvpPos.addRule(RelativeLayout.RIGHT_OF,R.id.rlNewId);
tvpEmail.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
tvpPos.addRule(RelativeLayout.LEFT_OF,R.id.tvCountId);
tvPos.setId(R.id.tvPosId);
tvPos.setTextColor(COLOR.Black);
tvEmail.setPadding(5, 5, 5, 0);
tvEmail.setTextSize(15);
tvEmail.setText(acct.get_pos());
tvEmail.setEllipsize(TextUtils.TruncateAt.END);
tvEmail.setSingleLine();
acctrl.addView(tvPos, tvpPos);
不确定这是否是最好的方法。它使用RelativeLayout
作为整体框。然后它使用另一个RelativeLayout
来获得球,并在左侧以两个不同高度TextViews
为中心。我甚至没有开始使用TOTAL JOBS部分,但可能需要另外一个内部RelativeLayout
或LinearLayout
来实现差异高度TextViews
的居中。
除了令人困惑的复杂性之外,目前的问题涉及球......
1)无论我尝试什么,我都无法让球在箱子中垂直居中。此代码将其放在左上角。
2)我不得不"软糖"使用基于TextViews
的一些负填充来球内两个不同高度CENTER_VERTICAL
的居中。不确定这是否是实现它的最佳方式。
任何有关如何修复我理想的结果的建议都会非常感激...我正在用这一个拉出我的头发。就像我说的那样,我尝试了各种不同的布局命令,但似乎我进入它们的距离越远,我就错过了一些定位,居中或重叠的关键元素。
...谢谢
答案 0 :(得分:1)
有图像所需的UI的xml代码 的 SampleActivity.java 强>
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.widget.TextView;
public class SampleActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sample_layout);
TextView textView = (TextView) findViewById(R.id.tvinitial);
String newName = "New";
SpannableString ss1 = new SpannableString(newName);
ss1.setSpan(new RelativeSizeSpan(0.6f), 0, newName.length(), 0); // set size
ss1.setSpan(new ForegroundColorSpan(Color.WHITE), 0, newName.length(), 0);
textView.setText("");
textView.append("15");
textView.append("\n");
textView.append(ss1);
}
}
可绘制XML 的 circle.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="35dp" />
<solid android:color="#4cc9df" />
<stroke android:width="1dp" android:color="#4cc9df" />
</shape>
</item>
</selector>
<强> round_corner.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="20dp" />
<solid android:color="@android:color/white" />
<stroke android:width="1dp" android:color="@android:color/white" />
</shape>
</item>
</selector>
<强> sample_layout.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e9e9e9"
android:orientation="vertical"
android:layout_gravity="center_vertical"
android:padding="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@drawable/round_corner"
android:padding="10dp">
<ImageView
android:id="@+id/arrow"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentRight="true"
android:background="@null"
android:src="@drawable/ic_loc_arrow"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@+id/arrow"
android:layout_marginLeft="10dp">
<TextView
android:id="@+id/tvinitial"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_centerVertical="true"
android:background="@drawable/circle"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:padding="5dp"
android:text="@string/new_name"
android:textColor="@android:color/white"
android:textSize="18sp"
android:textStyle="bold"/>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/tvinitial">
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:fontFamily="sans-serif-condensed"
android:text="ADAM JUNE"
android:textColor="#474747"
android:textSize="25sp"
android:textStyle="normal"/>
<TextView
android:id="@+id/second_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="25dp"
android:fontFamily="sans-serif-condensed"
android:text="Accountant"
android:textColor="#474747"
android:textSize="16sp"
android:textStyle="bold"/>
</FrameLayout>
</RelativeLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="@+id/arrow"
android:layout_toLeftOf="@+id/right_layout">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:gravity="center"
android:text="TOTAL JOBS"
android:textColor="#3881a4"
android:textSize="14sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:gravity="center"
android:text="125"
android:layout_marginTop="10dp"
android:textColor="#3881a4"
android:textSize="50sp"
android:textStyle="bold"/>
</FrameLayout>
</FrameLayout>
<View
android:id="@+id/right_layout"
android:layout_width="25dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_below="@+id/arrow"/>
</RelativeLayout>
</LinearLayout>
<强> ic_loc_arrow.png 强>
移动屏幕截图
我希望这能解决您的问题。