在 ConstraintLayout 的左侧和右侧都受约束的 TextView 上包装文本

Wrapping text on a TextView that's constrained both on the left and on the right in a ConstraintLayout

在一个 ConstraintLayout 中,我有 2 个 TextView 的并排,我希望右边的那个在文本变得太长时换行,以便它保持在右边左边 TextView,并且不会溢出容器。这是我现在的代码:

<androidx.constraintlayout.widget.ConstraintLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="10sp">

      ...

      <TextView
           android:id="@+id/buildingTypeLabel"
           style="@style/FormLabel"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:paddingTop="5sp"
           android:paddingBottom="5sp"
           android:text="@string/reference_view_building_type"
           app:layout_constraintLeft_toLeftOf="parent"
           app:layout_constraintTop_toBottomOf="@id/dateLabel"
           tools:text="Type de bâtiment" />

      <TextView
           android:id="@+id/buildingType"
           style="@style/FormValue"
           android:layout_width="0dp"
           android:layout_height="wrap_content"
           android:layout_gravity="end"
           android:layout_marginStart="5dp"
           android:layout_weight="1"
           android:ellipsize="none"
           android:maxLines="100"
           android:scrollHorizontally="false"
           app:layout_constraintBaseline_toBaselineOf="@id/buildingTypeLabel"
           app:layout_constraintRight_toRightOf="parent"
           app:layout_constraintStart_toEndOf="@+id/buildingTypeLabel"
           app:layout_constraintTop_toBottomOf="@id/date"
           tools:text="Bâtiments gouvernementaux" />
                ...
</androidx.constraintlayout.widget.ConstraintLayout>

但不是把text文字放在右边2行TextView,而是右边溢出:

请注意,我已尝试将 app:layout_constrainedWidth="true" 添加到右侧 TextView,但它并没有改变任何内容。

我怎样才能严格执行那个 TextView 的左右约束并让它在需要时将文本换行以具有更小的宽度?

对将文本扩展到设备屏幕之外的右侧文本视图使用 ellipsize= "end"。

使用指南 指南是约束布局的一部分 api。 文档在这里-> https://developer.android.com/reference/androidx/constraintlayout/widget/Guideline

这就是您在 xml 中定义指南的方式。

        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline_start"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:orientation="vertical"
            app:layout_constraintGuide_begin="?dialogPreferredPadding" />

指南可以作为您要隔离的两个文本视图之间的区别因素。

请参阅此 Whosebug 答案以获得更好的理解

上面link也会给大家介绍barriers,barriers也是constraint layout的一部分api,同样可以帮你解决上面的问题

您可以在下面找到解决方案:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10sp">

    <TextView
        android:id="@+id/buildingTypeLabel"
        style="@style/FormLabel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:paddingTop="5sp"
        android:paddingBottom="5sp"
        android:text="@string/reference_view_building_type"
        app:layout_constraintEnd_toStartOf="@id/buildingType"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/dateLabel"
        tools:text="Type de bâtiment" />

    <TextView
        android:id="@+id/buildingType"
        style="@style/FormValue"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="5dp"
        android:maxLines="100"
        app:layout_constraintBaseline_toBaselineOf="@id/buildingTypeLabel"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/buildingTypeLabel"
        app:layout_constraintTop_toBottomOf="@id/date"
        tools:text="Bâtiments gouvernementaux\n\nlef,le,flelf,le,fle," />

</androidx.constraintlayout.widget.ConstraintLayout>

尝试改为 layout_constraintEnd_toEndOf layout_constraintRight_toRightOf :)