RelativeLayout中往往需要定义每一个控件的资源ID
layout_toLeftOf layout_toRightOf layout_below表示相对位置
layout_alignXXX表示对齐方式
layout_marginXXX表示间距
android:layout_centerInParent="true":将控件放置于父界面的中心位置
即子控件的位置可以取决于兄弟控件或者父控件的位置,与LinearLayout相比,减少嵌套使用,界面代码简洁.
实例代码 : (下列代码效果是由以中间的控件为参照物来定位的)
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Java"
android:layout_centerInParent="true"
android:textSize="30sp"
android:textColor="#F000"
android:id="@+id/textView_center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左上"
android:textSize="30sp"
android:id="@+id/textView_left_top"
android:layout_above="@+id/textView_center"
android:layout_alignParentStart="true"
android:layout_marginBottom="60dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右上"
android:textSize="30sp"
android:id="@+id/textView_right_top"
android:layout_alignTop="@+id/textView_left_top"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左下"