安卓入门系列-05常见布局之RelaiveLayout(相对布局)
生活随笔
收集整理的這篇文章主要介紹了
安卓入门系列-05常见布局之RelaiveLayout(相对布局)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
相對(duì)布局(RelativeLayout)的使用
- 背景
- 接著上一篇提到的線性布局,如果說(shuō)線性布局是遵循一種順序排放,一處存在一個(gè)組件就不會(huì)存在另一個(gè)。那么相對(duì)布局則是位置上的相對(duì)關(guān)系(對(duì)于其他組件),不指定相對(duì)位置則會(huì)堆在一起重疊起來(lái)。
- 什么是相對(duì)布局
- 相對(duì)布局指的是有參照的布局方式,就是以某個(gè)兄弟組件,或者父容器來(lái)決定組件自己位置的。(兄弟組件是在同一個(gè)布局里面的組件,id參照其他布局里的組件會(huì)出問(wèn)題)
- 常見(jiàn)屬性
- android:gravity
- 設(shè)置容器內(nèi)各個(gè)子組件的對(duì)齊方式。
- android:ignoreGravity
- 這是子組件的屬性,設(shè)置了的話那么該子組件不受上面的gravity影響。
- 嘗試設(shè)置一個(gè)布局文件,如下。
- <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.zc.helloworld.MainActivity"><Buttonandroid:id="@+id/btn_01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="I am Button"/><Buttonandroid:id="@+id/btn_02"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="I am Button"/><Buttonandroid:id="@+id/btn_03"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="I am Button"/></RelativeLayout>
- 效果如下。驚訝發(fā)現(xiàn)三個(gè)按鈕只出現(xiàn)了一個(gè),這就是相對(duì)布局,沒(méi)有指定相對(duì)位置,那么就會(huì)疊放在一個(gè)位置。
- 設(shè)置權(quán)重,但是這只會(huì)改變組件對(duì)于布局的排列,而不能設(shè)置組件自身之間的關(guān)系,因此依然重疊。
- android:gravity
- 子組件的屬性
- 根據(jù)父容器確定位置
- 向左對(duì)齊:android:layout_alighParentLeft
- 向右對(duì)齊:android:layout_alighParentRight
- 頂端對(duì)齊:android:layout_alighParentTop
- 底部對(duì)齊:android:layout_alighParentBottom
- 水平居中:android:layout_centerHorizontal
- 垂直居中:android:layout_centerVertical
- 中央位置:android:layout_centerInParent(兩個(gè)方向)
- 屬性值為布爾值。
- 進(jìn)行設(shè)置如下。(取消權(quán)重設(shè)置,顯然不同于LinearLayout的必須給出排列方向,Relativelayout自定義性更強(qiáng))
- <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.zc.helloworld.MainActivity"><Buttonandroid:id="@+id/btn_01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:text="I am Button"/><Buttonandroid:id="@+id/btn_02"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:layout_alignParentTop="true"android:text="I am Button"/><Buttonandroid:id="@+id/btn_03"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:text="I am Button"/></RelativeLayout>
- 效果如下
- 根據(jù)兄弟組件確定位置
- 左邊:android:layout_toLeftOf
- 右邊:android:layout_toRightOf
- 上方:android:layout_above
- 下方:android:layout_below
- 對(duì)齊上邊界:android:layout_alignTop
- 對(duì)齊下邊界:android:layout_alignBottom
- 對(duì)齊左邊界:android:layout_alignLeft
- 對(duì)齊右邊界:android:layout_alignRight
- 屬性值均為兄弟組件id。
- 如下設(shè)置。
- <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.zc.helloworld.MainActivity"><Buttonandroid:id="@+id/btn_01"android:layout_toLeftOf="@+id/btn_02"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="I am Button"/><Buttonandroid:id="@+id/btn_02"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:layout_alignParentTop="true"android:text="I am Button"/><Buttonandroid:id="@+id/btn_03"android:layout_toRightOf="@id/btn_02"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="I am Button"/></RelativeLayout>
- 效果如下。
- 自身設(shè)置與父容器邊距確定位置
- android:layout_margin: 組件的四周外部留出一定的邊距
- android:layout_marginLeft: 組件的左邊外部留出一定的邊距
- android:layout_marginTop: 組件的上邊外部留出一定的邊距
- android:layout_marginRight: 組件的右邊外部留出一定的邊距
- android:layout_marginBottom: 組件的下邊外部留出一定的邊距
- 屬性值為像素值(px或者dp)
- 自身設(shè)置內(nèi)部元素邊距
- android:padding :組件的四周內(nèi)部留出一定的邊距
- android:paddingLeft: 組件的左邊內(nèi)部留出一定的邊距
- android:paddingTop: 組件的上邊內(nèi)部留出一定的邊距
- android:paddingRight: 組件的右邊內(nèi)部留出一定的邊距
- android:paddingBottom: 組件的下邊內(nèi)部留出一定的邊距
- 屬性值同上。
- 根據(jù)父容器確定位置
總結(jié)
以上是生活随笔為你收集整理的安卓入门系列-05常见布局之RelaiveLayout(相对布局)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 安卓入门系列-04常见布局之Linear
- 下一篇: 安卓入门系列-06常见布局之Constr