Android项目-定义编辑文本大小

Android项目-定义编辑文本大小,android,view,size,width,android-edittext,Android,View,Size,Width,Android 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)

嗨,伙计们,我正在设置编辑文本的宽度。 我的目标是将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);
但我找不到设置宽度的工作方法:(

谢谢
Marco

最好在布局中完成,不要使用代码

下面创建的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>

最好在布局中完成,不要使用代码

下面创建的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>

只要做:

userName.setWidth(half);
只要做:

userName.setWidth(half);
马可

EditText
继承
TextView
。这意味着您可以使用继承的方法。

Marco


EditText
继承
TextView
。这意味着您可以使用继承的方法。

谢谢,问题是返回的屏幕大小为0,所以我使用了此函数:Display Display=getWindowManager().getDefaultDisplay();int width=Display.getWidth();谢谢,问题是返回的屏幕大小是0,所以我使用了这个函数:Display Display=getWindowManager().getDefaultDisplay();int width=Display.getWidth();nevermind,以后自定义布局时一定要遵循我的建议=)nevermind,在将来定制布局时,请务必遵循我的建议=)