Pat*_*nan 4 android android-layout android-linearlayout
我有一个包含两个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>
Run Code Online (Sandbox Code Playgroud)
我希望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>
Run Code Online (Sandbox Code Playgroud)
但是......请记住,我改变了你的宽度LinearLayout:
android:layout_width="0dp"
android:layout_weight="1"
Run Code Online (Sandbox Code Playgroud)
至
android:layout_width="wrap_content"
Run Code Online (Sandbox Code Playgroud)
这是答案中非常重要的一部分.
我提供的代码是这样的:
[[short text][(s)]]
[[slightly longer text][(s)]]
[[really long ...ng text][(s)]]
Run Code Online (Sandbox Code Playgroud)
它正确管理椭圆化,但不能引入"空视图"(____).如果我没有更改LinearLayout's layout_width,椭圆化将正常工作((s)不会被推离屏幕),但文本对齐将不正确.它看起来像这样:
[[short text ][(s)]]
[[slightly longer text ][(s)]]
[[really long ...ng text][(s)]]
Run Code Online (Sandbox Code Playgroud)
因此,如果需要"空视图",则需要实现它(例如)并将其嵌套LinearLayout在另一个内部(此时"空视图"将是实际的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>
Run Code Online (Sandbox Code Playgroud)
这将给您以下行为:
[[[short text][(s)]]____________]
[[[slightly longer text][(s)]]__]
[[[really long ...ng text][(s)]]]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1268 次 |
| 最近记录: |