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

ConstraintLayout, когда ограничение, зависящее от ограничения, исчезло, представление макета ведет себя странно

Я использую ConstraintLayout, где я покажу, как показано ниже

enter image description here

Я хотел бы скрыть First (используя ушел) и тот вид, который, как я ожидаю, будет таким, как показано ниже (где ElasticBody растянется, чтобы использовать также оригинальное пространство вида First.

enter image description here

Однако, когда я на самом деле установил First на gone, мой вид оказался таким, как показано ниже (все изображение из представления Android Studio Design). Мой Elastic Body тоже отсутствует, а высота странно увеличена.

enter image description here

Мой код макета, как показано ниже

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

    <TextView
        android:id="@+id/txt_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#0ff"
        android:text="First"
        android:visibility="gone"
        android:textSize="26sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/txt_body"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/txt_body"
        android:layout_width="0dp"
        android:background="#f0f"
        android:layout_height="wrap_content"
        android:text="Elastic Body"
        android:textSize="26sp"

        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/txt_tail"
        app:layout_constraintStart_toEndOf="@+id/txt_first"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/txt_tail"
        android:background="#ff0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tail"
        android:textSize="26sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/txt_body"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

(Обратите внимание, если вы удалите gone, вы получите первый вид изображения). Почему это так? Как я могу исправить это, когда мой First исчезнет, я смогу правильно растянуть Elastic Body?

p/s: я знаю, как это сделать в LinearLayout и RelativeLayout... но интересно, если это ограничение для ConstraintLayout?

4b9b3361

Ответ 1

See Output :

Попробуйте следующее.

Установите первый вид слева и верхние ограничения на "родитель". После этого:

  • установите ширину вида тела в "0dp"
  • установите левое ограничение на первый вид правой стороны
  • установите правое ограничение для левой стороны.

Таким образом, всякий раз, когда вы устанавливаете видимость первого представления как "ушел", вид тела будет растягиваться так, как вы этого хотите.

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

    <TextView
        android:id="@+id/txt_first"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#0ff"
        android:text="First"
        android:textSize="26sp"
        android:visibility="gone"
        app:layout_constraintEnd_toStartOf="@+id/txt_body"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent" />

    <TextView
        android:id="@+id/txt_body"
        android:layout_width="0dp"
        android:background="#f0f"
        android:layout_height="wrap_content"
        android:text="Elastic Body"
        android:textSize="26sp"
        app:layout_constraintRight_toLeftOf="@+id/txt_tail"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/txt_first"
        />

    <TextView
        android:id="@+id/txt_tail"
        android:background="#ff0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Tail"
        android:textSize="26sp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

</android.support.constraint.ConstraintLayout>

Ответ 2

Еще одна вещь, которую вы можете использовать для Visibility.GONE в ConstraintLayout: Barriers.

Если вы не знаете Barriers, проверьте это: Барьеры