【发布时间】:2020-04-06 11:12:20
【问题描述】:
这个看似简单的布局问题我解决不了:
我有一个TextView,我想要一个带有图标的ImageView。我不能使用CompoundDrawable,因为该图标需要是可点击的。
TextView 也必须是 wrap_content,因为它有背景。
设置为TextView 的文本长度可变。如果文本很短,TextView 应该只占用它需要的空间,并且图标应该就在它旁边。如果文本很长,图标应该在最右边,TextView 占据它左边的所有空间。
这就是我想要的:
我尝试过的:
<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 会占用所有空间并将图标“推”出屏幕。
如何解决?
【问题讨论】:
标签: android android-layout android-constraintlayout