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 - 我如何/在哪里可以 "stash"/在我的应用程序中保存数据?

android - 无法获取提供商 com.google.android.gms.ads.MobileAdsInitProvider : java. lang.IllegalStateException

java - 如何修复 Android 中提供的layout_width属性

android - 为什么drawablePadding在Button中不起作用?

android - CoordinatorLayout 中的 ViewPager 涵盖 API >= 21 上的其他 View

android - 等到用户停止输入后再在搜索 View 中执行大量搜索

android - 如何在 ASP.NET Web Api 中使用安全性(身份验证和授权)

android - 有没有类似文件:///android_asset/that points to the apps directory?的路径

java - 在 Android 中裁剪和调整图像大小

Android圆圈背景变成椭圆形