6

我试图得到这样的东西:http: //img202.imageshack.us/img202/552/layoutoy.png。我将其用作列表项(技术上用作 ExpandableListView 的组视图)。

这是 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight">

    <TextView
        android:id="@+id/list_item_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end" />

    <Button
        android:id="@+id/list_item_button"
        android:text="Click me!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/list_item_text" />

</RelativeLayout>

但这不起作用。Button 不包装其内容,而是使用所有可用的水平空间。TextView确实包装了它的内容,但我想要它做的是在它与 Button 重叠时切断。

换句话说,我希望所有按钮都具有相同的宽度,而不管文本视图中的文本数量如何。这是可能吗?

4

2 回答 2

5

我认为你应该反过来尝试。使 TextView 位于按钮的左侧。这样 Textview 不会与 Button 重叠。如果您希望它在行尾被剪切,您必须将其限制为一行。目前它只会将其余文本移动到下一行。

这应该可以解决问题:

<Button
    android:id="@+id/list_item_button"
    android:text="Click me!"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"/>

<TextView
    android:id="@+id/list_item_text"
    android:text="veryveryveryveryveryveryveryveryveryverylong"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/list_item_button"
    android:ellipsize="end" />

</RelativeLayout>
于 2010-04-27T11:53:54.937 回答
4

在任何人尝试上面显示的方法(使用RelativeLayout)之前,您应该为此使用LinearLayout。权重应该正确设置:需要占用空白空间的元素的权重必须为 1,宽度设置为 fill_parent,需要保持其最小尺寸的元素的权重必须为 0,宽度为包装内容。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight">
<TextView
    android:id="@+id/list_item_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight=1
    android:ellipsize="end" />

<Button
    android:id="@+id/list_item_button"
    android:text="Click me!"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight=0/>

</LinearLayout>
于 2011-05-27T06:14:56.467 回答