2

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

我有一个 LinearLayout(水平的),我想在里面显示 2 个布局,一个在左侧,一个在右侧(它们本身有叠加层、按钮等......)。如果空间允许,我希望右边的总是完全显示,而左边的只有在右边完全显示后才显示。

更新

<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%,剩下的就用于左边的。请问我该怎么做?

非常感谢!

4

1 回答 1

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>
于 2012-12-06T23:13:23.740 回答