TextView 动态宽度,最后有一个视图

Mah*_*hag 2 android android-layout

我有两个元素, aTextView和 an ImageView。该ImageView会在的结束TextView。所述TextView的宽度将基于文本的变化,并且ImageView也遵循的结束TextView。像这样:

在此处输入图片说明

但问题是,如果文本太长,ImageView就会从屏幕上消失,就像这样:

在此处输入图片说明

但它应该是这样的:

在此处输入图片说明

如何仅通过 XML 获取此信息?我知道以编程方式可以实现,但我想要一个 XML 解决方案。

注意:ImageView可以是Button,所以我不能使用drawableEnd类似的解决方案。

Moh*_*laa 5

你可以试试这个,这里的关键是 app:layout_constrainedWidth="true"

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<TextView
    android:id="@+id/left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="lefttttttttttttttttttttttttttttttttttttttttttttttttt"
    app:layout_constrainedWidth="true"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toStartOf="@id/right"
    app:layout_constraintTop_toTopOf="parent"/>
 <ImageView
    android:id="@+id/right"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:src="@mipmap/ic_launcher"
    app:layout_constraintStart_toEndOf="@id/left"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@id/left"
    />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)