Как я могу заставить горизонтальные ограничения виджета получить приоритет над его вертикальными ограничениями?

Мне нужно построить квадрат, который всегда распространяется по всей ширине экрана (книжная ориентация), независимо от размера экрана. Под этим квадратом и некоторыми виджетами фиксированной высоты я хочу выделить оставшееся доступное пространство для текстового поля.

В приведенном ниже упрощенном коде показано, что я пробовал до сих пор:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ScMainFull"
>

<android.support.constraint.ConstraintLayout
    android:id="@+id/Grid"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="1:1"
    android:layout_margin="16dp"
    app:layout_constraintVertical_chainStyle="spread_inside"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/button1"
    />

<Button
    android:id="@+id/button1"
    android:layout_width="0dp"
    android:layout_height="48dp"
    android:text="1"
    android:layout_margin="16dp"
    app:layout_constraintTop_toBottomOf="@+id/Grid"
    app:layout_constraintBottom_toTopOf="@+id/Report"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    />

<TextView
    android:id="@+id/Report"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_margin="16dp"
    app:layout_constraintTop_toBottomOf="@+id/button1"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    />

</android.support.constraint.ConstraintLayout>

По вертикальной оси доступное пространство (общая высота за вычетом вертикальных полей и кнопки с фиксированной высотой) равномерно распределяется между двумя виджетами, которые имеют layout_height="0dp", игнорируя горизонтальные ограничения в первом виджете.

Что я должен изменить в этом коде, чтобы пространство, выделенное для текстового представления, было рассчитано после того, как квадрат был построен, чтобы занять всю ширину экрана? Другой способ задать тот же вопрос: как я могу заставить горизонтальные ограничения квадрата получить приоритет над его вертикальными ограничениями?


person ema3272    schedule 20.10.2017    source источник
comment
Вы вынуждены использовать ConstraintLayout? В противном случае вы могли бы добиться того, чего хотите, используя вертикальный LinearLayout.   -  person Michele    schedule 20.10.2017
comment
Да, родитель должен быть ConstraintLayout. Это позволяет мне использовать layout_constraintDimensionRatio="1:1", чтобы легко получить квадрат. Другие представления, не показанные в приведенном выше упрощенном коде, также требуют, чтобы родителем был ConstraintLayout. Квадратный вид может быть любым типом виджета. Я пробовал с View, но проблема та же.   -  person ema3272    schedule 20.10.2017


Ответы (1)


Просто снимите квадратную вершину ConstraintLayout с цепи.

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <android.support.constraint.ConstraintLayout
        android:id="@+id/Grid"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="1"
        android:layout_margin="16dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="48dp"
        android:text="1"
        android:layout_margin="16dp"
        app:layout_constraintVertical_chainStyle="spread_inside"
        app:layout_constraintTop_toBottomOf="@+id/Grid"
        app:layout_constraintBottom_toTopOf="@+id/Report"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />

    <TextView
        android:id="@+id/Report"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="16dp"
        app:layout_constraintTop_toBottomOf="@+id/button1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        />

</android.support.constraint.ConstraintLayout>

Вы делаете это неправильно, если думаете о вложении другой ViewGroup.

person Rami Jemli    schedule 21.10.2017