安卓 ConstraintLayout:约束结束不起作用

问题描述

我需要用 ConstraintLayout 创建链。我希望 TextView 附加到左侧,文本后紧跟一个 ImageView,另一个 ImageView 附加到屏幕右侧。查看示例。

enter image description here

如果 TextView 包含长文本,我需要将文本转到另一行。也就是说,TextView 不会与右侧的图像视图重叠,但仅限于边距。查看示例。

enter image description here

这是我的代码

<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"
    tools:ignore="MissingPrefix">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text"
        android:textColor="@color/black"
        android:textSize="@dimen/text_size_14"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0" />

    <ImageView
        android:id="@+id/ivFirst"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="16dp"
        android:src="@drawable/ic_first"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/ivSecond"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintStart_toEndOf="@id/tvTitle"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0" />

    <ImageView
        android:id="@+id/ivSecond"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_second"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0" />
</androidx.constraintlayout.widget.ConstraintLayout>

如果文本不长,那么一切正常,但如果文本很长,那么它会叠加在ImageView上并超出屏幕。我尝试使用链,但没有任何效果。请帮帮我。

解决方法

你可以用 layout_constrainedWidth 做到这一点。为此,您的文本视图需要水平约束。试试下面的布局,我对它做了一些改动。

<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"
tools:ignore="MissingPrefix">

<TextView
    android:id="@+id/tvTitle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:textSize="14sp"
    app:layout_constrainedWidth="true"
    app:layout_constraintEnd_toStartOf="@+id/ivFirst"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0" />

<ImageView
    android:id="@+id/ivFirst"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="16dp"
    android:src="@mipmap/ic_launcher"
    app:layout_constraintEnd_toStartOf="@id/ivSecond"
    app:layout_constraintStart_toEndOf="@id/tvTitle"
    app:layout_constraintTop_toTopOf="parent"
     />

<ImageView
    android:id="@+id/ivSecond"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@mipmap/ic_launcher"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
     />
    </androidx.constraintlayout.widget.ConstraintLayout>
,

您可以使用 FlexboxLayout 来包装 tvTitle TextView 和 ivFirst ImageView;以便它控制将 TextView 的内容包装到下一行,并避免将 ivFirst 推到右边/末尾。

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.flexbox.FlexboxLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        app:alignContent="center"
        app:alignItems="center"
        app:flexDirection="row"
        app:flexWrap="nowrap"
        app:layout_constraintBottom_toBottomOf="@+id/ivSecond"
        app:layout_constraintEnd_toStartOf="@+id/ivSecond"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/text"
            android:textColor="@color/black"
            android:textSize="@dimen/text_size_14"
            app:layout_flexShrink="10000" />

        <ImageView
            android:id="@+id/ivFirst"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_first"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="16dp"/>

    </com.google.android.flexbox.FlexboxLayout>

    <ImageView
        android:id="@+id/ivSecond"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_second"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

还要确保在模块级别添加 gradle 依赖项:

implementation 'com.google.android.flexbox:flexbox:3.0.0'

预览:

enter image description here

,

您可以通过将 ivSecond 固定到父级的末尾,然后创建 tvTitleivFirst 的水平链来实现这一点,只要您应用 packed链样式和对链的偏向 0 并在文本视图上使用 constrainedWidth

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

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constrainedWidth="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/ivFirst"
        app:layout_constraintHorizontal_bias="0"
        app:layout_constraintHorizontal_chainStyle="packed"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="hello world"/>

    <ImageView
        android:id="@+id/ivFirst"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/ivSecond"
        app:layout_constraintStart_toEndOf="@id/tvTitle"
        app:layout_constraintTop_toTopOf="parent"/>

    <ImageView
        android:id="@+id/ivSecond"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

当文本很短时,第一张图片就在它的对面:

enter image description here

当文本很长时,第一个图像停在第二个图像的边缘,文本换行到另一行:

enter image description here