如果在ConstraintLayout中文本太长,则TextView会超过Constraint

问题描述 投票:0回答:1

我有一个TextView向左对齐并垂直居中。两组ImageViewTextView都向右对齐,具有两种布局配置,一组配置在右侧,另一组配置在左侧。

您好世界文字可能会很长。我希望它扩展以填充整个宽度,而不会覆盖左侧的文本。

我添加了app:layout_constraintStart_toEndOf="@id/text1"来约束右边的组(图像+文本),使其不与左边的文本重叠。但是,它的行为不符合我的预期。

如何仅使用ConstraintLayout使其不重叠?

result with short text

Text1将被遮盖,如果正确的文本太长,这是不期望的。请注意,第二行上的图像也消失了。result with long text

代码:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Text1" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/row1_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toStartOf="@id/row1_image"
        app:layout_constraintHorizontal_bias="1"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toEndOf="@id/text1"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Hello World" />

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/row1_image"
        android:layout_width="22dp"
        android:layout_height="22dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/row1_text"
        app:layout_constraintTop_toTopOf="parent"
        tools:src="@drawable/ic_confirm" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/horizontal_barrier"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:barrierDirection="bottom"
        app:constraint_referenced_ids="row1_text,row1_image" />


    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/row2_image"
        android:layout_width="22dp"
        android:layout_height="22dp"
        app:layout_constraintEnd_toStartOf="@id/row2_text"
        app:layout_constraintHorizontal_bias="1"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toEndOf="@id/text1"
        app:layout_constraintTop_toTopOf="@id/horizontal_barrier"
        tools:src="@drawable/ic_confirm" />

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/row2_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/row2_image"
        app:layout_constraintTop_toTopOf="@id/horizontal_barrier"
        tools:text="Hello World" />

</androidx.constraintlayout.widget.ConstraintLayout>

如果正确的文本太长,则预期如下。它可以通过LinearLayout和RelativeLayout实现enter image description here

android android-layout constraints android-constraintlayout
1个回答
0
投票

请检查Constraintlayout版本。使用app:layout_constrainedWidth =“ true”与android:layout_width =“ wrap_content”

© www.soinside.com 2019 - 2024. All rights reserved.