android - 在 ConstraintLayout 中使用障碍链接

标签 android android-constraintlayout

我想使用没有嵌套的 ConstraintLayout 实现下面的布局。

enter image description here

布局需要一个屏障,因为不能保证哪个 textview 会更高。它还需要一条链,因为一切都需要居中。问题是我找不到一种方法来使链条与障碍一起工作。

这是我到目前为止的布局,但不会居中:

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

如果有人知道如何实现这一点,那将不胜感激!

最佳答案

我知道它很旧,但我认为要在没有嵌套的情况下做到这一点,您必须将顶级组件更改为这样的东西。

android:layout_height="wrap_content" android:layout_gravity="center"

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

关于android - 在 ConstraintLayout 中使用障碍链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55699062/

相关文章:

android - 在网络浏览器中打开链接

android - Android Studio 中的 "cannot resolve symbol R"

Android组件展示/视觉指南

android - 如何在自动调整 Android TextView 时出现 `wrap_content`?

android - 以编程方式添加约束

java - 为什么在调用 FinishAfterTransition 时会抛出 NoSuchMethodError?

android - 如何在android中禁用卡片 View 周围的阴影

android - 如何使用 ConstraintLayout 在 android studio 中添加垂直分隔线

android - 在 ConstraintLayout 中对 Google map 进行动画处理会使应用程序卡住

android - 为什么链元素之间的空间如此之大?