在ConstraintLayout中与障碍物链接

时间:2019-04-16 00:46:46

标签: 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>

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

2 个答案:

答案 0 :(得分:0)

  1. 使用horizontal chaintopLeftText创建topRightText,将链样式设置为packed,以使文本水平居中,并在{{1 }}在文本之间留出一定的水平空间
  2. 将{{1}的end margin设置为topLeftText的{​​{1}},以使文本顶部对齐。
  3. 使用top constrainttopLeftText创建top,将链样式设置为topRightText以使文本垂直居中,将vertical chain添加到bottomText以允许一些文本文字之间的垂直空间。
  4. topRightText添加边距以停止文本接触边缘。

屏幕截图

constraint layout center center texts

XML

bottomText

答案 1 :(得分:0)

我知道它已经很老了,但我认为这样做无需嵌套,您必须将顶层组件更改为类似的东西。

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">
相关问题