android - 如果 ConstraintLayout 中的文本太长,TextView 会超出 Constraint

标签 android android-layout constraints android-constraintlayout

我有一个 TextView 向左对齐并垂直居中。两组ImageViewTextView均右对齐,有两种布局配置,一组图片在右,一组图片在左。

Hello World 文本可以很长。我希望它扩展以填满整个宽度而不覆盖左侧文本。

我添加了 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

最佳答案

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

关于android - 如果 ConstraintLayout 中的文本太长,TextView 会超出 Constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60624306/

相关文章:

Android:如何使用 ArrayAdapter 获取微调项的 ID

android - Dagger 2 androidx fragment 不兼容类型

java - 如何使用android :inputType ="textPassword"在密码EditText中输入非英文文本

database - Postgres : constraint check and null values

matrix - 使用特征值求解稀疏矩阵

android - Android ListView动画化,像通知列表一样上下滑动

android - 有什么简单的方法可以在 Android 中更改 Spinner 下拉菜单的颜色?

android - 以编程方式将边距添加到 RelativeLayout

android - 在 Android 中播放视频时出现黑屏

ios - 使用比例约束会导致不同屏幕尺寸上的故障