【问题标题】:how to make an EditText have a maximum width but still fill parent width如何使 EditText 具有最大宽度但仍填充父宽度
【发布时间】:2013-09-18 07:25:33
【问题描述】:

我有一个 EditText,我想填充所有可用的水平宽度,但宽度不应大于 200dp

这使得 EditText 可以适应任何屏幕尺寸,并且仍然使它在大屏幕上看起来不错(水平拉伸在大屏幕上看起来不好看)。

如何在 Android 中做到这一点?

我看到maxWidth=200dplayoutWidth=fill_parent 不能一起工作:

 <EditText android:id="@+id/oldpassword"
           android:hint="@string/youroldpassword"
           android:inputType="textpassword"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:maxWidth="250dp" />

如果maxWidthlayoutWidth=fill_parent 不能一起使用,那么maxWidth 是什么意思?

换句话说,如果 EditBox 不动态改变它的宽度,那么你需要maxWidth 做什么?

【问题讨论】:

  • 请添加整个布局,以便我们了解此布局的目的..
  • 它在一个简单的 LinearLayout 中,layout_width 和 layout_height 设置为“fill_parent”
  • 当你设置 maxWidth 时,宽度不超过给定的限制,当你设置 minWidth 时,它保持给定的限制甚至更少或没有内容
  • 如果您想用 EdiText 填充其余区域,请从您的代码中删除 maxWidth 属性。

标签: android android-layout


【解决方案1】:

您可以根据设备屏幕尺寸以编程方式设置宽度。 喜欢

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
EditText et = (EditText)findViewById(R.id.editText);
if(width > 200)
{
    et.setWidth(200);
}
else
{
    et.setWidth(width);
}

【讨论】:

  • 我很害怕。这是一件不寻常的事情吗?另外,我对 maxWidth 的含义感到困惑。如果它不能与 fill_parent 一起使用,那它有什么用?
  • 因为您需要动态设置它,所以我无法处理 xml 文件。好的,我仍在尝试替代选项。等等……
【解决方案2】:

我有解决办法, 看看你是否设置了android:layout_width="fill_parent",它的宽度总是有父级,但是当你设置android:layout_width="wrap_content"时,当你在EditText中输入文本时,它会随着内容增加大小宽度,现在如果你使用android:maxWidth="250dp"android:layout_width="wrap_content"然后在 EditText 中输入值时,它会将其宽度增加到 250dp

使用

<EditText android:id="@+id/oldpassword"
       android:hint="@string/youroldpassword"
       android:inputType="textpassword"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:maxWidth="250dp" />`

或使用

<EditText android:id="@+id/oldpassword"
       android:hint="@string/youroldpassword"
       android:inputType="textpassword"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"/>

【讨论】:

  • 我需要介于两者之间的东西。我希望 EditText 填充父级,但其宽度不应超过 250dp。不过你的解释很有用,谢谢
  • 然后您可以定义 layout_width="250dp" 修复,否则没有任何解决方案可以提供帮助。
【解决方案3】:

删除布局中的 maxWidth=200dp

【讨论】:

  • 你看我的问题了吗?如何使 EditText 不会水平拉伸超过 200dp?
  • 发布您的布局文件
【解决方案4】:

您可以将编辑文本包装到相对布局中。这是您可以执行的操作 -:

<RelativeLayout
    android:id="@+id/relativeLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/oldpassword"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="250dp"
        android:maxLines="1"
        />
</RelativeLayout>

【讨论】:

    【解决方案5】:

    您可以将editText包装在布局中(宽度为200dp)并将editText设置为match_parent。

    【讨论】:

    • 有什么区别? EditText 宽度将固定为 200dp
    • 请参考这个帖子:stackoverflow.com/questions/13325403/…,看最后一个答案的cmets
    • 它没有回答我的问题,EditText 没有水平拉伸
    【解决方案6】:

    我用android:layout_marginRight="300dp"解决了,这个值越大,EditText对象的行宽越小。

    【讨论】:

    • 这不是解决方案,因为 Android 支持 8000+ 设备和 300 dp 它非常大,您的布局在某些设备上应该非常无用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 2012-09-29
    • 2011-07-16
    • 2014-07-04
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多