android - 无法在文本更改时调整项目组并尊重边距

标签 android android-layout android-view android-constraintlayout

我正在使用以下布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent">

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="start"
        app:constraint_referenced_ids="right_txt1,right_txt2,right_txt3"/>


    <TextView
        android:id="@+id/right_txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bar"
        android:gravity="end"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/left_txt1"
        app:layout_constraintBottom_toBottomOf="@id/left_txt1"
        />

    <TextView
        android:id="@+id/right_txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bar Bar"
        android:gravity="end"
        app:layout_constraintTop_toBottomOf="@id/c1"
        app:layout_constraintEnd_toEndOf="parent"
        />

    <TextView
        android:id="@+id/right_txt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bar"
        app:layout_constraintTop_toBottomOf="@id/c2"
        app:layout_constraintEnd_toEndOf="parent"
        />


    <View
        android:id="@+id/view1"
        android:layout_width="120dp"
        android:layout_height="10dp"
        android:background="@color/green"
        app:layout_constraintTop_toTopOf="@id/right_txt1"
        app:layout_constraintBottom_toBottomOf="@id/right_txt1"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        />

    <View
        android:id="@+id/view2"
        android:layout_width="120dp"
        android:layout_height="10dp"
        android:background="@color/green"
        app:layout_constraintTop_toTopOf="@id/right_txt2"
        app:layout_constraintBottom_toBottomOf="@id/right_txt2"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        />

    <View
        android:id="@+id/view3"
        android:layout_width="120dp"
        android:layout_height="10dp"
        android:background="@color/green"
        app:layout_constraintTop_toTopOf="@id/right_txt3"
        app:layout_constraintBottom_toBottomOf="@id/right_txt3"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        />

    <TextView
        android:id="@+id/left_txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="start"
        app:layout_constrainedWidth="true"
        app:layout_constraintTop_toTopOf="@id/view1"
        app:layout_constraintBottom_toBottomOf="@id/view1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/view1"
        app:layout_constraintHorizontal_bias="0"
        android:text="FooBar"
        />

    <androidx.constraintlayout.widget.Constraints
        android:id="@+id/c1"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        app:layout_constraintTop_toBottomOf="@id/left_txt1"
        app:layout_constraintStart_toStartOf="parent"
        />

    <TextView
        android:id="@+id/left_txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constrainedWidth="true"
        app:layout_constraintTop_toTopOf="@id/view2"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/c1"
        app:layout_constraintBottom_toBottomOf="@id/view2"
        app:layout_constraintEnd_toStartOf="@id/view2"
        android:text="FooBarBaz"
        />

    <androidx.constraintlayout.widget.Constraints
        android:id="@+id/c2"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        app:layout_constraintTop_toBottomOf="@id/left_txt2"
        app:layout_constraintStart_toStartOf="parent"
        />

    <TextView
        android:id="@+id/left_txt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"

        app:layout_constraintTop_toTopOf="@id/view3"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/left_txt2"
        app:layout_constraintBottom_toBottomOf="@id/view3"
        app:layout_constraintEnd_toStartOf="@id/view3"
        android:text="FooBar"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

结果是:
enter image description here

这对于这个例子来说没问题,但我想要的是随着文本的增长,各个项目作为一个整体进行调整而不会失去对齐。无法正常工作的示例:
enter image description here

而且边距不起作用
例如。添加android:layout_marginBottom="18dp"left_txt1没有效果或添加 android:layout_marginBottom="18dp"to the left_txt2`有如下效果

enter image description here

我希望随着文本的增长,下面/上面的项目作为一个整体给出空间

我在这里搞砸了什么?

最佳答案

为了使左侧的 TextViews 不相互重叠,您需要在它们之间创建垂直约束。然后每个进度条和右边的 TextView 应该根据相应的左边 TextView 的高度来限制,而不是相反,如另一个答案中所述。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent">

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="start"
        app:constraint_referenced_ids="right_txt1,right_txt2,right_txt3"
        />

    <TextView
        android:id="@+id/right_txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/left_txt1"
        app:layout_constraintBottom_toBottomOf="@id/left_txt1"
        />

    <TextView
        android:id="@+id/right_txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bar Bar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/left_txt2"
        app:layout_constraintBottom_toBottomOf="@id/left_txt2"
        />

    <TextView
        android:id="@+id/right_txt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Bar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/left_txt3"
        app:layout_constraintBottom_toBottomOf="@id/left_txt3"
        />

    <View
        android:id="@+id/view1"
        android:layout_width="120dp"
        android:layout_height="10dp"
        android:background="@color/green"
        app:layout_constraintTop_toTopOf="@id/left_txt1"
        app:layout_constraintBottom_toBottomOf="@id/left_txt1"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        />

    <View
        android:id="@+id/view2"
        android:layout_width="120dp"
        android:layout_height="10dp"
        android:background="@color/green"
        app:layout_constraintTop_toTopOf="@id/left_txt2"
        app:layout_constraintBottom_toBottomOf="@id/left_txt2"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        />

    <View
        android:id="@+id/view3"
        android:layout_width="120dp"
        android:layout_height="10dp"
        android:background="@color/green"
        app:layout_constraintTop_toTopOf="@id/left_txt3"
        app:layout_constraintBottom_toBottomOf="@id/left_txt3"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        />

    <TextView
        android:id="@+id/left_txt1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="start"
        app:layout_constrainedWidth="true"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/left_txt2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/view1"
        app:layout_constraintHorizontal_bias="0"
        android:text="FooBar"
        />

    <TextView
        android:id="@+id/left_txt2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constrainedWidth="true"
        app:layout_constraintTop_toBottomOf="@id/left_txt1"
        app:layout_constraintBottom_toTopOf="@id/left_txt3"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/view2"
        app:layout_constraintHorizontal_bias="0"
        android:text="FooBarBaz"
        />

    <TextView
        android:id="@+id/left_txt3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constrainedWidth="true"
        app:layout_constraintTop_toBottomOf="@id/left_txt2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@id/view3"
        app:layout_constraintHorizontal_bias="0"
        android:text="FooBar"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

关于android - 无法在文本更改时调整项目组并尊重边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58570153/

相关文章:

java - 我在 android studio 上有一个 R.Layout.main 错误

android - 运行gradle clean显示ProcessLogger失败

android - 来自 google play 的产品的不同订单 ID

android - 哪种布局将使用 10,1"1024x600 平板电脑?

android - 在我的自定义 View 上重复使用标准的 android 属性

android - 我可以用一个 View 创建一个没有包含布局的 xml 文件吗

android - 有没有办法检查状态栏的可见性?

android - 来自 viewPager 的 fragment 隐藏在操作栏的选项卡后面

Android - 如何将 DP 转换为 PX - 与绝对像素无关的密度

android - 无法在自定义 SurfaceView 类之上进行绘制