在 RelativeLayout 中分配额外的 space
Distributing extra space in RelativeLayout
我正在尝试构建一个 RelativeLayout
来在其子项之间分配多余的 space,如下所示:
我已经弄清楚如何将红色小部件置于蓝色小部件的中心,但我还想做的是将任何多余的垂直 space 均匀分布在蓝色和橄榄色小部件的上方、下方和之间.目前,一切都集中在顶部。
我的 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>
首先,您正在使用 layout_weight
和 RelativeLayout
parent。它没用,因为 layout_weight
仅适用于 LinearLayout
parent.
其实我没听懂你的问题(最好有布局图),但我想这对你有用:
<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>
我正在尝试构建一个 RelativeLayout
来在其子项之间分配多余的 space,如下所示:
我已经弄清楚如何将红色小部件置于蓝色小部件的中心,但我还想做的是将任何多余的垂直 space 均匀分布在蓝色和橄榄色小部件的上方、下方和之间.目前,一切都集中在顶部。
我的 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>
首先,您正在使用 layout_weight
和 RelativeLayout
parent。它没用,因为 layout_weight
仅适用于 LinearLayout
parent.
其实我没听懂你的问题(最好有布局图),但我想这对你有用:
<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>