问题描述
我需要在布局中并排放置两个文本视图,并且必须遵守两个规则:
I have two textviews i need to put side by side in a layout and I have to respect two rules:
-
Textview2必须始终完整显示.
Textview2 needs always to be displayed entirely.
Textview1.
Textview1 has to be cropped if there is no enough room in the layout.
示例:
Textview1 | textview2
Textview1|textview2
Teeeeeeeeeeeeeeeeeeeeeeeextview1 ... | textview2
Teeeeeeeeeeeeeeeeeeeextview1...|textview2
有什么想法吗?
我发现唯一可行的方法是用textview2的文本创建一个可绘制对象,并将其作为coumpoundDrawable应用于textview1.
The only way I found that might work is to create a drawable with the text of textview2 and affect it as coumpoundDrawable to textview1.
推荐答案
将两个TextView包裹在LinearLayout中.将布局权重0分配给textview2,将布局权重1分配给textview2.
Wrap the two TextViews in a LinearLayout. Assign a layout weight of 0 to textview2 and a layout weight of 1 to textview2.
有关更多信息,请参见此处:线性布局权重
See here for more info: Linear Layout Weight
如果使用下面的示例,您会看到LinearLayout首先将空间分配给textview2(权重为0),然后将剩余的空间分配给textview1(权重为1).如果没有足够的空间容纳两个TextView,则先将textview1省略.在下面的示例中,只有当LinearLayout小于textview2本身的大小时,textview2才会变为椭圆形.为FrameLayout分配特定的布局宽度,然后看看会发生什么.
If you play with the example below you'll see that the LinearLayout first allocates space to textview2 (with weight 0) and then allocates whatever remains to textview1 (with weight 1). If there is insufficient space to accommodate both TextViews, textview1 will be ellipsized first. In the example below textview2 will only ever become ellipsized if the LinearLayout is smaller than the size of textview2 itself. Assign a specific layout width to the FrameLayout and see what happens.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="match_parent"
android:background="#FF0000FF">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFF0000"
android:ellipsize="end"
android:maxLines="1"
android:text="textview1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#FF00FF00"
android:ellipsize="end"
android:maxLines="1"
android:text="textview2" />
</LinearLayout>
</FrameLayout>
这篇关于将两个textview并排放置在布局中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!