我对Android很新,我尝试做非常基本的事情。 我想有一个包含2个textFields
的布局这就是我现在所做的:
// LinearLayout
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
// TextView1
final TextView textView1 = new TextView(this);
textView1.setText(R.string.app_name);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
textView1.setBackgroundColor(ContextCompat.getColor(this, R.color.red));
LinearLayout.LayoutParams lyp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 100);
linearLayout.addView(textView1, lyp1);
// TextView2
final TextView textView2 = new TextView (this);
textView2.setText(R.string.app_name);
textView2.setBackgroundColor(ContextCompat.getColor(this, R.color.blue));
LinearLayout.LayoutParams lyp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
linearLayout.addView(textView2, lyp2);
但我不知道如何说第二个应该是第一个之后的juste,直到布局的底部。
我该怎么办?
谢谢
答案 0 :(得分:1)
不要使用相对布局来完成此类任务。它比LinearLayout慢,可以使用LinearLayout轻松完成。使用layoutWeight,如下例所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="100dp" android:layout_width="match_parent"
/>
<TextView
android:layout_height="0dp" android:layout_width="match_parent"
android:layout_weight="1" />
</LinearLayout>
要从代码设置layout_weight,请执行以下操作:
public static int dpToPx(int dp)
{
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// LinearLayout
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
// TextView1
final TextView textView1 = new TextView(this);
textView1.setText(R.string.app_name);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
textView1.setBackgroundColor(Color.RED);
LinearLayout.LayoutParams lyp1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, dpToPx(100));
linearLayout.addView(textView1, lyp1);
// TextView2
final TextView textView2 = new TextView (this);
textView2.setText(R.string.app_name);
textView2.setBackgroundColor(Color.BLUE);
LinearLayout.LayoutParams lyp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
linearLayout.addView(textView2, lyp2);
setContentView(linearLayout);
}
答案 1 :(得分:0)
首先,你应该为这个任务使用线性布局。但是如果你想把它放在RelativeLayout中,那么解决方案就在
之下1 - 您应该将第二个textView的高度设置为match_parent
2-RelativeLayout.END_OF会将布局与textview1的右侧对齐。所以使用RelativeLayout.BOTTOM
3-RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,100);将你的textview的高度设置为100像素(像素大小因设备而异)。所以在dp中设置高度就像这样
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float dp = displayMetrics.density;
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, (int)(100*dp));
尝试这个 -
//density of device
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
float dp = displayMetrics.density;
// RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
// TextView1
final TextView textView1 = new TextView(this);
textView1.setText(R.string.app_name);
textView1.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
textView1.setBackgroundColor(ContextCompat.getColor(this, R.color.red));
RelativeLayout.LayoutParams lyp1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, (int)(100*dp));
lyp1.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
relativeLayout.addView(textView1, lyp1);
// TextView2
final TextView textView2 = new TextView (this);
textView2.setBackgroundColor(ContextCompat.getColor(this, R.color.blue));
RelativeLayout.LayoutParams lyp2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
lyp2.addRule(RelativeLayout.BOTTOM, textView1.getId());
relativeLayout.addView(imageView1, lyp2);
mainLinearLayout.addView(relativeLayout);