ConstraintLayout - динамически устанавливаемые ограничения

Базовый пример нескольких ConstrainLayout:

<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:id="@+id/constLayout_root"
    >

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constLayout_A"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:background="#ff0"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constLayout_B"
        android:layout_width="30dp"
        android:layout_height="40dp"
        android:background="#f0f"
        android:visibility="gone"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constLayout_C"
        android:layout_width="30dp"
        android:layout_height="50dp"
        android:background="#0f0"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/constLayout_A"
        />

</android.support.constraint.ConstraintLayout>

constLayout_B накладки constLayout_A, а также constLayout_C теперь отображается ниже constLayout_A.

Теперь мне нужно программно изменить ограничения и отобразить constLayout_C ниже constLayout_B (и скрыть constLayout_A).

Я попытался связать их таким образом:

final ConstraintSet cs_C = new ConstraintSet();
cs_C.connect(constLayout_C.getId(), ConstraintSet.TOP, constLayout_B.getId(), ConstraintSet.BOTTOM);
cs_C.connect(constLayout_C.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
cs_C.connect(constLayout_C.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
cs_C.connect(constLayout_C.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
cs_C.applyTo(constLayout_C);

final ConstraintSet cs_B = new ConstraintSet();
cs_B.connect(constLayout_B.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
cs_B.connect(constLayout_B.getId(), ConstraintSet.BOTTOM, constLayout_C.getId(), ConstraintSet.TOP);
cs_B.connect(constLayout_B.getId(), ConstraintSet.START, ConstraintSet.PARENT_ID, ConstraintSet.START);
cs_B.connect(constLayout_B.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END);
cs_B.applyTo(constLayout_B);

Мой ConstraintSetпо какой-то причине не применяется. Как я могу изменить верхнее ограничениеconstLayout_C динамически?

2 ответа

Решение

Ваш макет B перекрывается с макетом A, потому что вы устанавливаете app:layout_constraintTop_toBottomOf атрибуты к parent. И установка поля ограничения макета программным способом не является хорошей практикой. установите свой макет следующим образом

<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="wrap_content"
android:id="@+id/constLayout_root"
>

<android.support.constraint.ConstraintLayout
    android:id="@+id/constLayout_A"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_marginTop="24dp"
    android:background="#ff0"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.501"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.constraint.ConstraintLayout
    android:id="@+id/constLayout_B"
    android:layout_width="30dp"
    android:layout_height="40dp"
    android:layout_marginTop="17dp"
    android:background="#f0f"
    android:visibility="gone"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/constLayout_A" />

<android.support.constraint.ConstraintLayout
    android:id="@+id/constLayout_C"
    android:layout_width="30dp"
    android:layout_height="50dp"
    android:layout_marginTop="16dp"
    android:background="#0f0"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.498"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/constLayout_B" />

</android.support.constraint.ConstraintLayout>

И помните, что цель использования constrainLayout заключается в том, что он дает нам возможность перетаскивать представление и устанавливать его в макете, а не рассматривать его как программную установку.

Ограничение B в макетах XML имеет видимость Gone.

<android.support.constraint.ConstraintLayout
    android:id="@+id/constLayout_B"
    android:layout_width="30dp"
    android:layout_height="40dp"
    android:background="#f0f"
    android:visibility="gone"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />

Измените это на VISIBLEв XML или во время ограничения набора изменений в коде. Убедитесь, что вы используетеConstraintSet.clone() перед применением изменений.

Другие вопросы по тегам