Подтвердить что ты не робот

MaxWidth не работает с fill_parent

При использовании 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>
4b9b3361

Ответ 1

Атрибут 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"/>

Ответ 2

Вы можете добиться такого же эффекта, используя разные макеты для разных экранов. Скажите, что вы хотите, чтобы ваш 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"/>

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"/>

Ответ 3

Если кому-то match_parent совместное поведение match_parent и maxWidth в одном макете: вы можете использовать ConstraintLayout и ограничить дочерний layout_constraintWidth_max родительскими границами и добавить ограничение 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"
    android:padding="@dimen/login_unrelated_element_margin">

    <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>