RelativeLayout是實際布局中最常用的布局方式之一。RelativeLayout可以設置某一個視圖相對於其他視圖的位置,這些位置可以包括上下左右等,因而相較於其他的布局方式而言具有很大的靈活性。
RelativeLayout的常用屬性有以下一些:
第一類:屬性值為true或false
android:layout_centerHrizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInparent 相對於父元素完全居中
android:layout_alignParentBottom 貼緊父元素的下邊緣
android:layout_alignParentLeft 貼緊父元素的左邊緣
android:layout_alignParentRight 貼緊父元素的右邊緣
android:layout_alignParentTop 貼緊父元素的上邊緣
android:layout_alignWithParentIfMissing 如果對應的兄弟元素找不到的話就以父元素做參照物
第二類:屬性值必須為id的引用名“@id/id-name”
android:layout_below 在某元素的下方
android:layout_above 在某元素的的上方
android:layout_toLeftOf 在某元素的左邊
android:layout_toRightOf 在某元素的右邊
android:layout_alignTop 本元素的上邊緣和某元素的的上邊緣對齊
android:layout_alignLeft 本元素的左邊緣和某元素的的左邊緣對齊
android:layout_alignBottom 本元素的下邊緣和某元素的的下邊緣對齊
android:layout_alignRight 本元素的右邊緣和某元素的的右邊緣對齊
第三類:屬性值為具體的像素值
android:layout_marginBottom 離某元素底邊緣的距離
android:layout_marginLeft 離某元素左邊緣的距離
android:layout_marginRight 離某元素右邊緣的距離
android:layout_marginTop 離某元素上邊緣的距離
下面是一個使用相對布局的實例。activity_main.xml源碼如下:

1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" > 5 6 <!-- 居中的按鈕:參照物 --> 7 <Button 8 android:id="@+id/mButton_center" 9 android:text="@string/center" 10 android:layout_centerHorizontal="true" 11 android:layout_centerVertical="true" 12 android:layout_width="90dp" 13 android:layout_height="wrap_content" > 14 </Button> 15 16 <!-- 上 --> 17 <Button 18 android:id="@+id/mButton_above" 19 android:text="@string/above" 20 android:layout_above="@id/mButton_center" 21 android:layout_centerHorizontal="true" 22 android:layout_width="90dp" 23 android:layout_height="wrap_content" > 24 </Button> 25 26 <!-- 下 --> 27 <Button 28 android:id="@+id/mButton_below" 29 android:text="@string/below" 30 android:layout_below="@id/mButton_center" 31 android:layout_centerHorizontal="true" 32 android:layout_width="90dp" 33 android:layout_height="wrap_content" > 34 </Button> 35 36 <!-- 左 --> 37 <Button 38 android:id="@+id/mButton_left" 39 android:text="@string/left" 40 android:layout_toLeftOf="@id/mButton_center" 41 android:layout_centerVertical ="true" 42 android:layout_width="120dp" 43 android:layout_height="wrap_content" > 44 </Button> 45 46 <!-- 右 --> 47 <Button 48 android:id="@+id/mButton_right" 49 android:text="@string/right" 50 android:layout_toRightOf="@id/mButton_center" 51 android:layout_centerVertical ="true" 52 android:layout_width="120dp" 53 android:layout_height="wrap_content" > 54 </Button> 55 56 <!-- 左上 --> 57 <Button 58 android:id="@+id/mButton_aboveAndleft" 59 android:text="@string/aboveAndleft" 60 android:layout_above="@id/mButton_center" 61 android:layout_toLeftOf="@id/mButton_above" 62 android:layout_width="120dp" 63 android:layout_height="wrap_content" > 64 </Button> 65 66 <!-- 右上 --> 67 <Button 68 android:id="@+id/mButton_aboveAndright" 69 android:text="@string/aboveAndright" 70 android:layout_above="@id/mButton_center" 71 android:layout_toRightOf="@id/mButton_above" 72 android:layout_width="120dp" 73 android:layout_height="wrap_content" > 74 </Button> 75 76 <!-- 左下 --> 77 <Button 78 android:id="@+id/mButton_belowAndleft" 79 android:text="@string/belowAndleft" 80 android:layout_below="@id/mButton_center" 81 android:layout_toLeftOf="@id/mButton_below" 82 android:layout_width="120dp" 83 android:layout_height="wrap_content" > 84 </Button> 85 86 <!-- 右下 --> 87 <Button 88 android:id="@+id/mButton_belowAndright" 89 android:text="@string/belowAndright" 90 android:layout_below="@id/mButton_center" 91 android:layout_toRightOf="@id/mButton_below" 92 android:layout_width="120dp" 93 android:layout_height="wrap_content" > 94 </Button> 95 </RelativeLayout>
效果圖如圖1所示:
圖1:Android_RelativeLayout實例