大家好我正在尝试设置EditText的宽度。 我的目标是将EditText的长度设置为整个屏幕的1/2。
我写了这段代码:
RelativeLayout rl = (RelativeLayout) this.findViewById(R.id.mainRelativeLayout);
int width = rl.getWidth();
int half = width/2;
EditText userName = (EditText) this.findViewById(R.id.userName);
userName.SET_SOMEHOW_THE_SIZE(half);
但我找不到设定宽度的工作方法:(
由于 马可
答案 0 :(得分:3)
最好在布局中完成,不要使用代码
以下创建一个EditText,占用半个屏幕,TextView占用另一半,只是学会玩布局,它们应该让你得到你期望的结果
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
答案 1 :(得分:1)
只是做:
userName.setWidth(half);
答案 2 :(得分:1)
马科,
EditText
继承TextView
。这意味着您可以使用继承的setWidth()
方法。