【问题标题】:Wrapping text on a TextView that's constrained both on the left and on the right in a ConstraintLayout在 ConstraintLayout 的左侧和右侧都受约束的 TextView 上包装文本
【发布时间】:2020-10-12 10:51:27
【问题描述】:

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>

但不是将文本文本放在右侧TextView 的两行上,而是在右侧溢出:

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

如何严格执行 TextView 的左右约束,并在需要时将文本换行以具有更小的宽度?

【问题讨论】:

  • 尝试改成layout_constraintEnd_toEndOf而不是layout_constraintRight_toRightOf
  • @P.Juni 您的答案是迄今为止最简单的。我有混合的开始/结束和左/右约束。您应该将其添加为答案,以便我可以选择它作为正确答案

标签: android android-layout android-constraintlayout


【解决方案1】:

尝试更改为layout_constraintEnd_toEndOf 而不是layout_constraintRight_toRightOf :)

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      使用指南 指南是约束布局 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" />
      

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

      请参阅此 stackoverflow 答案以获得更好的理解 What is difference between Barrier and Guideline in Constraint Layout?

      上面的链接还将向您介绍障碍,它也是约束布局api的一部分,也可以帮助您解决上述问题。

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 2012-06-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-02-06
          • 1970-01-01
          • 2018-08-04
          相关资源
          最近更新 更多