【发布时间】: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