【问题标题】:How do i give an editText a fixed width我如何给一个editText一个固定的宽度
【发布时间】:2016-04-01 17:18:16
【问题描述】:

我如何给我的 EditText-s 一个固定的宽度。这是我的代码

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingBottom="4dp" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Date:"
                android:layout_weight="0"
                android:textColor="#FFBBB3B9" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textColor="#c91f1f"
                android:saveEnabled="true"
                android:id="@+id/e1b"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Time:"
                android:layout_weight="0"
                android:textColor="#FFBBB3B9" />

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textColor="#c91f1f"
                android:id="@+id/e1c"
                android:saveEnabled="true"
                 />
        </LinearLayout>

我试过属性 android:maxLines 和 android:maxWidth 但我无法解决问题。

谢谢。

【问题讨论】:

标签: android xml


【解决方案1】:

您可以使用代码固定编辑文本的宽度:

EditText dittext = (EditText)findViewById(R.id.sometextview);
edittext.setMaxWidth(edittext.getWidth());

【讨论】:

  • 'setMaxWidth' 和 'getWidth' 给出无法解决符号错误。
  • 检查您的代码。它工作正常。我的解决方案代码已经过测试
  • 避免将代码直接输入到“MainActivity”类中。即确保您在方法中编写它。
【解决方案2】:

在 .xml 文件中你可以这样做:

<EditText ... android:layout_width="28dp" .../>

【讨论】:

  • 我不想使用固定宽度,因为不同设备的屏幕尺寸会发生变化。
  • dp 作为度量非常灵活。并且您可以针对不同的方向和屏幕尺寸使用不同的。
【解决方案3】:

LinearLayout 的 android:orientation 是水平的。 您可以将其更改为垂直。

android:orientation="vertical"

【讨论】:

  • 很好的建议,但我想保持方向,因为这个布局只是 NestedScrollView 的一部分。
猜你喜欢
  • 2013-09-03
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-02
相关资源
最近更新 更多