我有两个 TextView 需要在布局中并排放置,我必须遵守两条规则:
Textview2 需要始终完全显示。
如果布局中没有足够的空间,则必须裁剪 Textview1。
例子:
TextView 1| TextView 2
Teeeeeeeeeeeeeeeeeeeextview1...|textview2
有什么想法吗?
我发现可能可行的唯一方法是使用 textview2 的文本创建一个可绘制对象,并将其作为与 textview1 的 coumpoundDrawable 影响。
最佳答案
将两个 TextView 包装在一个 LinearLayout 中。将布局权重 0 分配给 textview2,将布局权重 1 分配给 textview2。
查看这里了解更多信息:Linear Layout Weight
如果您使用下面的示例,您会看到 LinearLayout 首先将空间分配给 textview2(权重为 0),然后将剩余的空间分配给 textview1(权重为 1)。如果没有足够的空间容纳两个 TextView,则 textview1 将首先被椭圆化。在下面的示例中,只有当 LinearLayout 小于 textview2 本身的大小时,textview2 才会变成椭圆形。为 FrameLayout 分配一个特定的布局宽度,看看会发生什么。
<?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>
关于android - 将两个 TextView 并排放置在布局中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21778632/