TextView动态宽度,末尾带有视图 [英] TextView dynamic width with a view at the end
问题描述
我有两个元素,一个TextView
和ImageView
. ImageView
将位于TextView
的末尾. TextView
的宽度将根据文本而变化,并且ImageView
也跟随TextView
的结尾.像这样:
I have two elements, a TextView
and an ImageView
. The ImageView
will be at the end of the TextView
. The TextView
's width will be change based on the text, and the ImageView
also follows the end of the TextView
. Like this:
但是问题是,如果文本太长,则ImageView
会从屏幕上消失,就像这样:
But the problem is, if the text is so long, the ImageView
goes out of the screen, like this:
但是应该是这样的:
如何仅通过XML获得此信息?我以编程方式知道可以实现,但是我想要一个XML解决方案.
How can I get this only by XML? I know programatically it can be achieved, but I want a XML solution.
注意:ImageView
可以是Button
,所以我不能使用drawableEnd
之类的解决方案.
Note: The ImageView
can be a Button
, so I can't use drawableEnd
like solution.
推荐答案
您可以尝试一下,这里的键是app:layout_constrainedWidth="true"
you can try this the key here is 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>
这篇关于TextView动态宽度,末尾带有视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!