1. 实验内容简介
相对布局可以设置某一个视图相对于其他视图的位置,这些位置包括上、下、左、右。设置这些位置的属性是android:layout_above、android:layout_below、android:layout_toLeftOf、android:layout_toRightOf。除此之外,还可以通过android:layout_alignBaseline属性设置视图的底端对齐。这5个属性的值必须是存在的资源ID,也就是另一个视图的android:id属性值。
2. UI界面布局
对应的大纲如下:
3. XML代码编写和调试
<?xml version="1.0"encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:textSize="16dp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button1"
android:layout_toRightOf="@id/button1"
android:text="Button2"
android:textSize="16dp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button2"
android:layout_toLeftOf="@id/button2"
android:text="Button3"
android:textSize="16dp" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/button2"
android:layout_toRightOf="@id/button2"
android:text="Button4"
android:textSize="16dp" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/button2"
android:layout_toRightOf="@id/button2"
android:text="Button5"
android:textSize="16dp" />
</RelativeLayout>