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

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

如何在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吗?

最佳答案

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

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);
}

10-07 19:33
查看更多