我正在尝试获取一行文本,类似于
foofoofoo - barbarbar
但如果它不能适合一行,则我想用省略号来替换foo和bar。 即我正在尝试缩短它们的长度。
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="0dip"
android:layout_weight="1"
android:textColor="#ffffff"
android:singleLine="true"
android:ellipsize="true"
android:text="foofoofoofoo" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:text=" - " />
<TextView
android:id="@+id/text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="0dip"
android:layout_weight="1"
android:textColor="#ffffff"
android:singleLine="true"
android:ellipsize="true"
android:text="barbarbarbar" />
</LinearLayout>
请耐心等待,这个方法只能部分起作用。
将
TextView
的layout_width
设置为0dip
,layout_weight
设置为1
,意味着它们将各占可用空间的50%。将
singleLine
设置为true
,将ellipsize
设置为true
,意味着如果文本大于容器,则它们将看起来像foofoo...。因此,如果文本较长,则我的结果应该是
这就是我想要的! 所以这个方法有效。 现在我要解决的问题是,如果第一个foofoo.. - barbar..
TextView
(id:text_1)的文本小于layout_width="0dip"
和layout_weight="1"
所给出的50%,我希望它可以使用wrap_content
。否则它看起来像:
我希望得到的结果是:foo 空格 - barbar..
这就是为什么我已经更改了foo - barbarbar
layout_width="wrap_content"
并添加了android:maxWidth="0dip"
,但这没有起作用!它似乎忽略了我说的wrap_content
,仍然给这个视图50%!我读到需要添加
android:adjustViewBounds="true"
才能使maxWidth
起作用,但这没有影响。