我有一个包含两个 TextView 的 LinearLayout。设第一个 TextView 的文本为“短文本”,第二个 TextView 的文本为“(s)”。
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/variable-size"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:ellipsize="middle"
android:lines="1"
android:singleLine="true"
android:text="short text"/>
<TextView
android:id="@+id/fixed-size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:ellipsize="none"
android:lines="1"
android:singleLine="true"
android:text="(s)" />
</LinearLayout>
我希望 LinearLayout 这样显示给用户:
[[短文本][(s)]____________]
其中 ____ 表示空 View 。
现在,如果我将稍长的字符串放入第一个 TextView,我希望看到:
[[稍长的文本][(s)]__]
如果我将更长的字符串放入第一个 TextView,我希望看到:
[[很长...ng 文本][(s)]]
但我似乎无法找到一种方法来防止第一个 TextView 完全排挤第二个 TextView,如下所示:
[[真的很长……很长的文字]]
如何获得我正在寻找的行为?
最佳答案
你的问题的答案是这样的:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/variable-size"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="middle"
android:lines="1"
android:singleLine="true"
android:text="short text" />
<TextView
android:id="@+id/fixed-size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:ellipsize="none"
android:lines="1"
android:singleLine="true"
android:text="(s)" />
</LinearLayout>
但是... 请记住,我将 LinearLayout 的宽度从:
android:layout_width="0dp"
android:layout_weight="1"
到
android:layout_width="wrap_content"
这是答案中非常重要的一部分。
我给出的代码是这样工作的:
[[short text][(s)]]
[[slightly longer text][(s)]]
[[really long ...ng text][(s)]]
它可以正确管理省略号,但不能引入“空 View ”(____)。如果我没有更改 LinearLayout 的 layout_width,椭圆化会正常工作((s) 不会被推出屏幕),但文本对齐方式不正确。它看起来像这样:
[[short text ][(s)]]
[[slightly longer text ][(s)]]
[[really long ...ng text][(s)]]
因此,如果您需要“空 View ”,您将需要(例如)通过将 LinearLayout 嵌套在另一个 View 中来实现它(这次“空 View ”将是一个实际的 View)。像这样:
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/variable-size"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:ellipsize="middle"
android:lines="1"
android:singleLine="true"
android:text="short text" />
<TextView
android:id="@+id/fixed-size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:ellipsize="none"
android:lines="1"
android:singleLine="true"
android:text="(s)" />
</LinearLayout>
<View
android:id="@+id/empty_view"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
这会给你以下行为:
[[[short text][(s)]]____________]
[[[slightly longer text][(s)]]__]
[[[really long ...ng text][(s)]]]
关于安卓布局 : How to keep right-most text element and ellipsize left-most text element as it grows?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32593845/