我正在尝试获取一行文本,类似于
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"
给出的 50%和 layout_weight="1"
我想要它 wrap_content
.否则它看起来像:
foo blank space - barbar..
我要
foo - barbarbar
这就是为什么我改变了layout_width="wrap_content"
并添加了 android:maxWidth="0dip"
但这不起作用!好像不理我说wrap_content
并且仍然给这个 View 50%!
我读到您需要添加 android:adjustViewBounds="true"
对于 maxWidth
工作,但这没有影响。
最佳答案
这是我能做的最好的,尝试更改字符串以查看它是否按预期工作。
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:shrinkColumns="0,2"
android:stretchColumns="0,2">
<TableRow>
<TextView android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:text="foofoofoo"/>
<TextView android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:gravity="center_horizontal"/>
<TextView android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="end"
android:text="barbarbarbarbarbarbarbarbarbarbarbar"/>
</TableRow>
</TableLayout>
关于使用 layout_weight、layout_width 和 maxWidth 的 Android 布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4960073/