在RelativeLayout中分配额外空间

时间:2015-10-14 21:37:46

标签: android android-layout android-relativelayout

我正在尝试构建一个RelativeLayout,它在子节点之间分配多余的空间,如下所示:

RelativeLayout example

我已经想出如何将红色小部件置于蓝色小部件的中心,但我还想做的是在蓝色和橄榄色小部件的上方,下方和之间均匀分布任何多余的垂直空间。目前,一切都在最顶层。

我的XML看起来大致如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    >

    <ImageButton android:id="@+id/blue_button"
        android:src="@drawable/blue_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_alignParentTop="true"
        />
    <ImageView
        android:id="@+id/red_box"
        android:src="@drawable/red_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/blue_button"
        android:layout_marginLeft="10dp"
        android:layout_alignTop="@id/blue_button"
        android:layout_alignBottom="@id/blue_button"
        />

    <ImageButton
        android:id="@+id/green_button"
        android:src="@drawable/green_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/blue_button"
        android:layout_weight="1"
        />

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

首先,您使用的是layout_weightRelativeLayout。它没用,因为layout_weight仅适用于LinearLayout父级。

其实我不明白你的问题(想要布局的图像会很好)但我想这对你有用:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    >

   <RelativeLayout
       android:id="@+id/blueAndRedWrapperLay"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="0dp"
       android:layout_weight="1"
       >

       <ImageButton android:id="@+id/blue_button"
           android:src="#00CCFF"
           android:layout_width="300dp"
           android:layout_height="200dp"
           android:layout_weight="1"
           android:layout_centerInParent="true" />

       <ImageView
           android:id="@+id/red_box"
           android:src="#ff0000"
           android:layout_width="100dp"
           android:layout_height="70dp"
           android:layout_centerInParent="true"
           />

       </RelativeLayout>

    <RelativeLayout
        android:id="@+id/greenWrapperLay"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        >

    <ImageButton
        android:id="@+id/green_button"
        android:src="#669900"
        android:layout_width="300dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true"
        />

    </RelativeLayout>

</LinearLayout>