在一个TextView使用“ellipsize”,只有在必要时伸展视图 [英] Using 'ellipsize' in a TextView and stretch the view ONLY if necessary
问题描述
我已经连续2 TextViews和2个要求:
I have 2 TextViews in a row and 2 requirements:
1)如果第一个TextView的是不是太宽,它看起来应该如下:
1) If the first TextView is not too wide, it should look as follows
| [1文本] [2文本]                |
|[1 text][2 text] |
2)如果第一个TextView的太宽,它看起来应该如下:
2) If the first TextView is too wide, it should look as follows
| [1文本文本特...] [2文本] |
|[1 text text tex...][2 text]|
第二个要求很简单,你可以使用Android:layout_weight =1,例如:
The second requirement is simple, you can use android:layout_weight="1", e.g.:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="end"
android:text="1 text text text text text"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2 text"
/>
</LinearLayout>
,但如果第一TextView中包含了一个简短的文字,它看起来像
, but if the first TextView contains a short text, it looks like
| [1文本&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;] [2文本] |
|[1 text ][2 text]|
,这是不能接受的。
因此,如何同时满足1),2)在同一时间要求吗?
So how to satisfy both 1) and 2) requirements at the same time?
推荐答案
在我发现了一个很简单的解决方案其间:只需设定的LinearLayout宽度为WRAP_CONTENT,而不是FILL_PARENT
In the meantime I found a very simple solution: just set the LinearLayout width to "wrap_content" instead of "fill_parent".
<LinearLayout
android:orientation="horizontal"
android:layout_width="WRAP_CONTENT"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="end"
android:text="1 text text text text text"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2 text"
/>
</LinearLayout>
这篇关于在一个TextView使用“ellipsize”,只有在必要时伸展视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!