1

非常简单的布局:我在一行中显示了两个 TextView。布局以屏幕为中心,两个字符串以编程方式设置,第一个是可变长度字符串,而第二个字符串为空或(比方说)“X”:

    <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:gravity="center_horizontal"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/lblSelectionName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="@string/empty"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/lblSelectionAttribute"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/empty"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

请注意,第一个 TextView 是椭圆的,以便在其大小超过 TextView 时切断字符串。我期待类似的东西:

“普通文本 | X”(以屏幕为中心)

“必须是...| X 的超大文本”(以屏幕为中心 - 填满屏幕)

但是会发生什么:第一个 TextView 缩小但填满了整个屏幕(-width),第二个 TextView 不在屏幕上(分别包装到下一行)。无论如何,使用“重量”也无济于事。将 layout_height 设置为固定值(例如 15dp)也无济于事......

有什么建议吗?

4

1 回答 1

2
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:orientation="horizontal"
    android:layout_gravity="center_horizontal"
     >

    <TextView
        android:id="@+id/lblSelectionName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ellipsize="end"
        android:singleLine="true"
        android:text="asdhsagdkgfhgfhghg"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/lblSelectionAttribute"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="X"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>
于 2013-04-05T09:41:15.450 回答