我有一个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
上的TextView
从match_parent
更改为wrap_content
,则会显示所有三个View
,但第三个Button
会显示layout_width
占用额外的空间,这不是我想要的。我还尝试将Button
上的0dp
设置为Button
,并尝试使用其他设置值,但都无济于事。
如何让两个TextView
不大于包装内容所需的{{1}},并让{{1}}占用额外的空间?
答案 0 :(得分:1)
添加有趣的android:layout_weight =&#34; x&#34;。
它的工作方式如下: 如果连续有4个视图,并且每个视图的权重为1.那么每个视图占据空间的1/4。如果一个人的体重为2,那么它占据2/5,依此类推。如果一个体重为0或没有体重,那么它只占据其内容所占的空间。系统将所有权重加在一起,然后将每个视图设置为比例宽度。
在你的情况下,你必须称他们为1,0,0。