正如你所說,把兩個元素放在RelativeLayout。
於是,「CENTER_HORIZONTAL」這兩個元素的屬性設置爲true,然後將綠色元素的「下面」屬性設置爲黑色元素的ID。
下面是完整的例子:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<View
android:id="@+id/view1"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="@color/Black"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<View
android:id="@+id/view2"
android:layout_height="100dp"
android:layout_below="@+id/view1"
android:background="@color/Green"
android:layout_centerHorizontal="true" />
</RelativeLayout>
( 「center_vertical」 是有點兒可選)
或者在這裏,不管對方意見位置:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<View
android:id="@+id/view1"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="@color/Black"
android:layout_centerVertical="true" />
<View
android:id="@+id/view2"
android:layout_width="40dp"
android:layout_height="100dp"
android:layout_below="@+id/view1"
android:layout_alignLeft="@+id/view1"
android:layout_alignRight="@+id/view1"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="@color/Green" />
</RelativeLayout>
(在這種情況下,邊距將定義第二個Views寬度)
這是最終的結果:
[從另一視圖的Android相對佈局alignCenter](可能重複http://stackoverflow.com/questions/5487864/android-relative-layout-aligncenter-from -another-view) – tir38