Android:简单的布局问题

时间:2012-12-06 23:05:08

标签: android layout

这可能是一个简单的布局问题,但我能够得到我想要的东西。

我有一个LinearLayout(水平),我想在里面显示2个Layouts,一个在左边,一个在右边(它们自己有覆盖,按钮等......)。我希望正确的一个完全显示,如果空间允许,只有在完全显示右边后才能显示左边的一个。

更新

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="20dp" >

    <RelativeLayout
        android:layout_width="0px"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:singleLine="true" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#000111" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerVertical="true" />
    </RelativeLayout>
</LinearLayout>

使用此代码,仅显示左侧。如果我将第一个RelativeLayout的权重更改为1,则两者都占据屏幕的50%,这是我所期望的。

我喜欢的是有权采取,如果需要100%,如果它需要少于100%,剩下的东西被用于左边的。我该怎么办呢?

非常感谢!

1 个答案:

答案 0 :(得分:2)

从右侧移除重量,并将左侧宽度设置为0,将重量设置为1:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="20dp" >

    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" >

        <!-- ..... -->

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >

        <!-- ...... -->

    </RelativeLayout>
</LinearLayout>