zmi*_*man 4 android android-layout
我有2个TextView连续和2个要求:
1)如果第一个TextView不是太宽,它应该如下所示
| [1 text] [2 text] |
2)如果第一个TextView太宽,它应该如下所示
| [1 text text tex ...] [2 text] |
第二个要求很简单,你可以使用android:layout_weight ="1",例如:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="end"
android:text="1 text text text text text"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2 text"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
,但如果第一个TextView包含一个短文本,它看起来像
| [1 text] [2 text] |
,这是不可接受的.
那么如何同时满足1)和2)的要求呢?
与此同时,我找到了一个非常简单的解决方案:只需将LinearLayout宽度设置为"wrap_content"而不是"fill_parent".
<LinearLayout
android:orientation="horizontal"
android:layout_width="WRAP_CONTENT"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:ellipsize="end"
android:text="1 text text text text text"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2 text"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1572 次 |
| 最近记录: |