使用fill_parent
时,maxWidth
无效。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxWidth="50dip"/>
</LinearLayout>
</LinearLayout>
最佳答案
maxWidth
属性对match_parent
(或已弃用的fill_parent
)的宽度没有影响,它们是互斥的。您需要使用 wrap_content
。
另外任何只有一个 child 的布局都可能被删除,例如,您可以将当前布局简化为:
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="50dip"/>
关于Android 使用线性布局 maxWidth,不适用于 fill_parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13325403/