Android 布局:如何在最右边的文本元素增长时保持最右边的文本元素并椭圆化最左边的文本元素?
Android Layout: How to keep right-most text element and ellipsize left-most text element as it grows?
我有一个包含两个 TextView 的 LinearLayout。设第一个 TextView 的文本为 "short text",第二个 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 这样显示给用户:
[[short text][(s)]____________]
其中 ____
表示空视图。
现在,如果我将稍长的字符串放入第一个 TextView,我希望看到:
[[slightly longer text][(s)]__]
如果我将更长的字符串放入第一个 TextView,我想看到这个:
[[really long ...ng text][(s)]]
但我似乎无法找到一种方法来防止第一个 TextView 完全排挤第二个 TextView,如下所示:
[[really long lo... long text]]
如何获得我正在寻找的行为?
你问题的答案是这样的:
<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)]]
它正确地管理了省略号,但不能引入 "empty view" (____
)。如果我不更改 LinearLayout's
layout_width
,省略号会正常工作((s)
不会被推离屏幕),但文本对齐不会正确。它看起来像这样:
[[short text ][(s)]]
[[slightly longer text ][(s)]]
[[really long ...ng text][(s)]]
因此,如果 "empty view" 是您的需要,您将需要(例如)将 LinearLayout
嵌套在另一个 "empty view" 中来实现它(这次 "empty 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)]]]
我有一个包含两个 TextView 的 LinearLayout。设第一个 TextView 的文本为 "short text",第二个 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 这样显示给用户:
[[short text][(s)]____________]
其中 ____
表示空视图。
现在,如果我将稍长的字符串放入第一个 TextView,我希望看到:
[[slightly longer text][(s)]__]
如果我将更长的字符串放入第一个 TextView,我想看到这个:
[[really long ...ng text][(s)]]
但我似乎无法找到一种方法来防止第一个 TextView 完全排挤第二个 TextView,如下所示:
[[really long lo... long text]]
如何获得我正在寻找的行为?
你问题的答案是这样的:
<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)]]
它正确地管理了省略号,但不能引入 "empty view" (____
)。如果我不更改 LinearLayout's
layout_width
,省略号会正常工作((s)
不会被推离屏幕),但文本对齐不会正确。它看起来像这样:
[[short text ][(s)]]
[[slightly longer text ][(s)]]
[[really long ...ng text][(s)]]
因此,如果 "empty view" 是您的需要,您将需要(例如)将 LinearLayout
嵌套在另一个 "empty view" 中来实现它(这次 "empty 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)]]]