maxWidth不适用于fill_parent

Evg*_*kov 19 user-interface android android-layout

使用时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>
Run Code Online (Sandbox Code Playgroud)

Sam*_*Sam 30

该属性maxWidth对宽度match_parent(或已弃用fill_parent)没有影响,它们是互斥的.你需要使用wrap_content.

此外,任何只有一个子项的布局都可能被删除,例如,您可以将当前布局简化为:

<?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"/>
Run Code Online (Sandbox Code Playgroud)

  • 这看起来很愚蠢,但它确实有效:添加`minWidth`并将"min"和"max"都设置为"500dp". (14认同)
  • 谢谢!如何填充我的EditText屏幕的所有宽度,但不是没有500dip?例如,某些屏幕的分辨率<500. (2认同)

Rol*_*den 16

如果有人想要单个父布局中match_parent和的组合行为maxWidth:您可以使用 ConstraintLayout 并通过与layout_constraintWidth_max.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText 
        android:layout_width="0dp"
        android:layout_height="wrap_content"                
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintWidth_max="480dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)


Kir*_*man 7

您可以使用不同的布局为不同的屏幕实现相同的效果.假设您希望EditText填充可用宽度但不超过480dp.将您的布​​局区分如下:

布局/ my_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)

layout-w480dp/my_layout.xml :(在这里选择最大宽度作为限定符)

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="480dp"
          android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud)