1.相对布局是从界面的左上角开始布局,因此连续两个相对布局中后一个会掩盖前一个
例如,红色被绿色遮盖
<?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">
<RelativeLayout
android:background="#ff0000"
android:layout_width="100dp"
android:layout_height="100dp"/>
<RelativeLayout
android:background="#00ff00"
android:layout_width="100dp"
android:layout_height="100dp"/>
</RelativeLayout>
2.根据父容器定位(以最外层为基础来定位)
使红色显现出来
<?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">
<RelativeLayout
android:background="#ff0000"
android:layout_alignParentRight="true"
android:layout_width="100dp"
android:layout_height="100dp"/>
<RelativeLayout
android:background="#00ff00"
android:layout_width="100dp"
android:layout_height="100dp"/>
</RelativeLayout>
3.根据兄弟容器定位
由于兄弟容器有很多,所以相对哪一个要设置id
给红色布局设置id
android:id="@+id/rl1"
注意是@+id
绿色布局使用时是@id
android:layout_toLeftOf="@id/rl1"
4.设置组件与父容器的边距
与左边相距100dp
android:layout_marginLeft="100dp"
上下左右偏移100dp
android:layout_margin="100dp"
5.设置组件内部元素的边距
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="100dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rl1"
android:background="#ff0000"
android:layout_centerInParent="true"
android:layout_width="100dp"
android:layout_height="100dp"/>
<RelativeLayout
android:background="#00ff00"
android:layout_width="100dp"
android:layout_height="100dp"/>
</RelativeLayout>