相对布局是指利用控件之间的相对位置关系来对布局进行设置。
在容器中的控件与其他任何一个控件或者容器都有相对关系。
例如:
<?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" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="14dp"
android:text="亲爱哒~" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp"
android:text="中秋快乐" />
<AnalogClock
android:id="@+id/analogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp" />
</RelativeLayout>
布局完成效果:
使用相对布局的方式主要是设置某个控件跟他周围的控件的相对位置关系:比如,它与**底部平齐,与某某控件顶部平齐,在某控件左边或者右边,在父控件中的相对位置关系。