RelativeLayout是一種相對布局脆诉,控件的位置是按照相對位置來計算的,后一個控件在什么位置依賴于前一個控件的基本位置伐坏,是布局最常用怔匣,也是最靈活的一種布局
相對布局里面的子元素不像線性布局里面的元素,不設(shè)置相關(guān)屬性他們就會以父元素的左上角為頂點疊加顯示
子元素居中 layout_centerInParent
<?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"
android:background="@android:color/white">
<TextView
android:id="@+id/middle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@android:color/holo_blue_bright"
android:text="我在最中間"/>
</RelativeLayout>
相對布局位置相對屬性都有一組桦沉,像layout_centerInParent這樣的性對于父元素的位置的屬性還有
android:layout_centerHorizontal="true" 相對于父元素在水平方向居中
android:layout_centerVertical="true" 相對于父元素垂直方向居中
android:layout_alignParentBottom="true" 相對于父元素居底部
android:layout_alignParentTop="true" 相對于父元素居頂部
android:layout_alignParentLeft="true" 相對于父元素居左部
android:layout_alignParentRight="true" 相對于父元素居右部
android:layout_alignParentEnd="true" 相對于父元素居右部 和 layout_alignParentRight 效果相同 api>=17
android:layout_alignParentStart="true" 相對于父元素居左部 和 layout_alignParentLeft 效果相同 api>=17
上面這些總結(jié)的屬性都是相對于父元素的位置
相對于元素外部四周
<?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"
android:background="@android:color/white">
<TextView
android:id="@+id/middle"
android:layout_width="200dp"
android:layout_height="200dp"
android:gravity="center"
android:layout_centerInParent="true"
android:background="@android:color/holo_blue_bright"
android:text="我在最中間"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/middle"
android:text="layout_below"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_green_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/middle"
android:text="layout_above"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_green_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/middle"
android:text="layout_toLeftOf"
android:layout_centerVertical="true"
android:background="@android:color/holo_green_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/middle"
android:text="layout_toRightOf"
android:layout_centerVertical="true"
android:background="@android:color/holo_green_light"/>
</RelativeLayout>
android:layout_below="@id/middle" 相對位置居元素外部的上方
android:layout_above="@id/middle" 相對位置居元素外部的下方
android:layout_toLeftOf="@id/middle" 相對位置居元素外部的左方
android:layout_toRightOf="@id/middle" 相對位置居元素外部的右方
上面這些總結(jié)的屬性都是相對于子元素外部的位置
相對于元素內(nèi)部四周
<?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"
android:background="@android:color/white">
<TextView
android:id="@+id/middle"
android:layout_width="200dp"
android:layout_height="200dp"
android:gravity="center"
android:layout_centerInParent="true"
android:background="@android:color/holo_blue_bright"
android:text="我在最中間"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/middle"
android:text="layout_alignBottom"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_orange_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/middle"
android:text="layout_alignTop"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_orange_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/middle"
android:text="alignLeft"
android:layout_centerVertical="true"
android:background="@android:color/holo_orange_light"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@id/middle"
android:text="alignRight"
android:layout_centerVertical="true"
android:background="@android:color/holo_orange_light"/>
</RelativeLayout>
android:layout_alignBottom="@id/middle" 相對位置居元素內(nèi)部的下方
android:layout_alignTop="@id/middle" 相對位置居元素內(nèi)部的上方
android:layout_alignLeft="@id/middle" 相對位置居元素內(nèi)部的左邊
android:layout_alignRight="@id/middle" 相對位置居元素內(nèi)部的右邊
上面這些總結(jié)的屬性都是相對于子元素內(nèi)部的位置
代碼只會按照你所寫的方式運行每瞒,不會按照你想的方式運行