我有一个视图,其中有两个LinearLayouts - 一个带有Text,EditText和一个按钮,另一个带有Text和EditText。我想获得第二个EditText的宽度(在第二行)以匹配第一行的宽度。我已经尝试使它成为一个TableLayout而不是仅仅使用LinearLayouts,现在我试图以编程方式设置宽度。我得到第一个EditText的宽度,并尝试第二个EditText的setWidth,但它不会更改大小。Android EditText以编程方式调整大小
* JAVA调整大小尝试*
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
getWidthBox = (EditText) findViewById(R.id.editText10);
setWidthBox = (EditText) findViewById(R.id.EditText01);
Toast.makeText(this, "Meaured Width: " + Integer.toString(getWidthBox.getMeasuredWidth()), Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Get Width: " + Integer.toString(getWidthBox.getWidth()), Toast.LENGTH_SHORT).show();
//Neither of these setWidth Lines Work Correctly
setWidthBox.getLayoutParams().width=getWidthBox.getWidth();
setWidthBox.setWidth(getWidthBox.getWidth());
}
* XML布局*
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:paddingLeft="5dp" >
<TextView
android:id="@+id/textView2"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:text="Points Allowed: "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15sp" />
<EditText
android:id="@+id/editText10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number" >
</EditText>
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="@drawable/editpencil" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.15"
android:paddingLeft="5dp" >
<TextView
android:id="@+id/TextView01"
android:layout_width="110dp"
android:layout_height="wrap_content"
android:text="Points Used: "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="15sp" />
<EditText
android:id="@+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number" />
</LinearLayout>
* FROM SCREENSHOT仿真器*
第二个EditText仍然会进入屏幕的边缘。 我附上了第一篇文章的截图。 – mattdonders 2012-07-15 14:50:18