Цепочка с барьером в ConstraintLayout

Я хочу добиться макета ниже, используя ConstraintLayout без вложенности.

введите описание изображения здесь

Для макета требуется барьер, поскольку не гарантируется, какой текст будет выше. Это также требует цепи, так как все должно быть центрировано. Проблема в том, что я не могу найти способ заставить цепь работать с барьерами.

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

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

    <TextView
        android:id="@+id/topLeftText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        tools:text="Test test test test test test test test test test"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/topRightText"/>

    <TextView
        android:id="@+id/topRightText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="16dp"
        android:layout_marginEnd="16dp"
        tools:text="Test test test test test test test test test test test test test test test test test test test test"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@+id/topLeftText"
        app:layout_constraintEnd_toEndOf="parent"/>

    <android.support.constraint.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:constraint_referenced_ids="topLeftText,topRightText"
        app:barrierDirection="bottom"/>

    <TextView
        android:id="@+id/bottomText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        tools:text="Test test test test test test test test test test test test test test test test test test test test"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/barrier"/>

</android.support.constraint.ConstraintLayout>

Если кто-нибудь знает, как этого добиться, это будет очень признательно!


person idunnololz    schedule 16.04.2019    source источник
comment
Вы пытались ограничить верхнюю часть верхнего левого текстового представления верхним правым текстовым представлением. Затем ограничьте верхний правый и нижний текстовые представления барьером   -  person ZeePee    schedule 06.03.2020
comment
Вы решили свою проблему, если да, то как? В настоящее время я сталкиваюсь с аналогичной проблемой, и единственное решение, о котором я могу думать иначе, - это использование вложенного макета. Что противоречит использованию ConstraintLayout, сохраняя плоскую иерархию.   -  person TreyWurm    schedule 17.03.2020
comment
Вы нашли обходной путь?   -  person mochadwi    schedule 24.04.2020
comment
Проблема, с которой вы столкнулись, заключается в том, что вы не можете центрировать все по вертикали? Если это так, возможно, вы можете попробовать использовать горизонтальную направляющую вместо барьера?   -  person Johan Ferreira    schedule 25.05.2020
comment
@TreyWurm Я использовал вложенный макет. Поскольку на этот вопрос до сих пор нет ответов, я думаю, что этого невозможно добиться без изменений в ConstraintLayout.   -  person idunnololz    schedule 12.11.2020


Ответы (1)


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

android:layout_height=wrap_content android:layout_gravity=центр

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto">
person Jakub S.    schedule 17.08.2020
comment
Вопрос в центрировании TextViews внутри ConstraintLayout. Ваш ответ касается центрирования ConstraintLayout внутри его родителя. Это вложение, потому что вы должны добавить такого родителя. - person Agent_L; 30.07.2021