【问题标题】:Android project - Define an EditText sizeAndroid 项目 - 定义 EditText 大小
【发布时间】:2011-08-28 01:06:32
【问题描述】:

大家好,我正在尝试设置 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);

但我找不到设置宽度的工作方法:(

谢谢 马可

【问题讨论】:

    标签: android view size width android-edittext


    【解决方案1】:

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

    下面创建一个占据半屏的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>
    

    【讨论】:

    • 没关系,以后在自定义布局时请务必遵循我的建议 =)
    【解决方案2】:

    马可,

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

    【讨论】:

    • 感谢问题是返回的屏幕尺寸为0,所以我使用了这个函数:Display display = getWindowManager().getDefaultDisplay(); int width = display.getWidth();
    【解决方案3】:

    只要做:

    userName.setWidth(half);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-11
      • 1970-01-01
      • 1970-01-01
      • 2015-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-06
      相关资源
      最近更新 更多