【发布时间】:2011-02-10 16:49:20
【问题描述】:
我正在尝试获取一行文本,类似于
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 工作,但这没有任何影响。
【问题讨论】:
标签: android android-layout textview android-layout-weight