除了可变宽度的 TextView 之外,ConstraintLayout 总是有 ImageView

ConstraintLayout always have ImageView besides TextView of variable width

我无法解决这个看似简单的布局问题:

我有一个 TextView,我想有一个 ImageView,右边有一个图标。我不能使用 CompoundDrawable,因为图标需要可点击。 另外 TextView 需要是 wrap_content,因为它有背景。

设置为 TextView 的文本是可变长度的。如果文本很短,TextView 应该只使用它需要的 space 并且图标应该紧挨着它。如果文本很长,图标应该在最右边,TextView 占据它左边的所有 space。

这就是我想要的:

我尝试过的:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#0000ff"
        android:text="short text"
        android:textColor="#ffffff"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@id/tv"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_close_black_24dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

这适用于短文本,但当文本很长时,TextView 会占据屏幕外的所有 space 和 'pushes' 图标。

如何解决?

这会起作用
[更新:- 使用水平线性布局]

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

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:weightSum="2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/textView57"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:layout_weight="1.8"
            android:text="TextView" />

        <ImageView
            android:id="@+id/imageView20"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_gravity="center"
            android:layout_marginTop="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:layout_weight="0.2"
            app:srcCompat="@drawable/filled_compass" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

要解决这个问题,您可以使用垂直方向的 Guideline 作为

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="@string/long_text"
        app:layout_constraintRight_toLeftOf="@id/guideline"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toRightOf="@id/guideline"
        app:layout_constraintTop_toTopOf="@id/tv"
        app:layout_constraintBottom_toBottomOf="@id/tv"
        android:src="@drawable/ic_launcher_background" 
        app:layout_constraintRight_toRightOf="parent"/>

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.7"/>

</androidx.constraintlayout.widget.ConstraintLayout>