Android RelativeLayout:无论我做什么,最右边的孩子都会占用所有额外的空间

时间:2014-03-15 05:37:53

标签: button textview relativelayout android-ui

我有一个RelativeLayout水平放置三个子视图。我希望最左边的一个占用任何额外的空间而另外两个只占用包装内容所需的空间。文档说这可以做到,但我不能使它工作。这就是我所拥有的:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="6dp"
    android:textStyle="bold"
    android:layoutDirection="rtl">

    <TextView
    android:id="@+id/nodeLabel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="@dimen/text_size"
    android:gravity="fill_horizontal"
    android:layout_alignParentLeft="true" />

    <Button
    android:id="@+id/launch"
    android:text="@string/launch_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/nodeLabel"
    android:gravity="left" />

    <Button
    android:id="@+id/kill"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/launch"
    android:layout_alignParentRight="true"
    android:text="@string/kill_label"
    android:gravity="left" />

</RelativeLayout>

结果是TextView占用了所有空间,而两个Buttons根本没有呈现。如果我将android:layout_width上的TextViewmatch_parent更改为wrap_content,则会显示所有三个View,但第三个Button会显示layout_width占用额外的空间,这不是我想要的。我还尝试将Button上的0dp设置为Button,并尝试使用其他设置值,但都无济于事。

如何让两个TextView不大于包装内容所需的{{1}},并让{{1}}占用额外的空间?

1 个答案:

答案 0 :(得分:1)

添加有趣的android:layout_weight =&#34; x&#34;。

它的工作方式如下: 如果连续有4个视图,并且每个视图的权重为1.那么每个视图占据空间的1/4。如果一个人的体重为2,那么它占据2/5,依此类推。如果一个体重为0或没有体重,那么它只占据其内容所占的空间。系统将所有权重加在一起,然后将每个视图设置为比例宽度。

在你的情况下,你必须称他们为1,0,0。