1 相對布局的概念
相對布局是通過相對定位的方式制定控件位置,即以其他控件或父容器為參照物擺放控件位置,在設計相對布局時,要遵循控件之間的依賴關系。
相對布局以一對<RelativeLayout></RelativeLayout>
標簽標識。
每個控件都可以有一個id屬性,用來標識自己的身份。
例如:為一個Button控件添加id
<Button id="@+id/btn_name/>
2 基本屬性
1 相對于父組件的位置,值為true 或 false
屬性 | 含義 |
---|---|
android:layout_alignParentTop | 將該控件的頂部與其父控件的頂部對齊; |
android:layout_alignParentBottom | 將該控件的底部與其父控件的底部對齊; |
android:layout_alignParentLeft | 將該控件的左部與其父控件的左部對齊; |
android:layout_alignParentRight | 將該控件的右部與其父控件的右部對齊; |
android:layout_centerHorizontal | 將該控件置于水平居中; |
android:layout_centerVertical | 將該控件置于垂直居中; |
android:layout_centerInParent | 將該控件置于父控件的中央; |
例如:設置按鈕id為btn_center的控件處于父控件的中央
<?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">
<Button
android:id="@+id/btn_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center"
android:layout_centerInParent="true"/>
</RelativeLayout>
2 相對于給定ID控件
屬性 | 含義 |
---|---|
android:layout_above | 將該控件的底部置于給定ID的控件之上; |
android:layout_below | 將該控件的底部置于給定ID的控件之下; |
android:layout_toLeftOf | 將該控件的右邊緣與給定ID的控件左邊緣對齊; |
android:layout_toRightOf | 將該控件的左邊緣與給定ID的控件右邊緣對齊; |
android:layout_alignBaseline | 將該控件的baseline與給定ID的baseline對齊; |
android:layout_alignTop | 將該控件的頂部邊緣與給定ID的頂部邊緣對齊; |
android:layout_alignBottom | 將該控件的底部邊緣與給定ID的底部邊緣對齊; |
android:layout_alignLeft | 將該控件的左邊緣與給定ID的左邊緣對齊; |
android:layout_alignRight | 將該控件的右邊緣與給定ID的右邊緣對齊; |
例如:設定id為btn_right的按鈕處于btn_center按鈕的右側,且與btn_center的基線對齊
<?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">
<Button
android:id="@+id/btn_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="right"
android:layout_toRightOf="@id/btn_center"
android:layout_alignBaseline="@id/btn_center"
/>
</RelativeLayout>
3 相對控件之間的間距屬性
屬性 | 含義 |
---|---|
android:layout_marginTop | 上偏移的值; |
android:layout_marginBottom | 下偏移的值; |
android:layout_marginLeft | 左偏移的值; |
android:layout_marginRight | 右偏移的值; |
例如:設定btn_right按鈕的左側邊距為20dp
<?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">
<Button
android:id="@+id/btn_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center"
android:layout_centerInParent="true"/>
<Button
android:id="@+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="right"
android:layout_toRightOf="@id/btn_center"
android:layout_alignBaseline="@id/btn_center"
android:layout_marginLeft="20dp"
/>
</RelativeLayout>
4 設置控件的內部間距
屬性 | 含義 |
---|---|
android:paddingTop | 設置布局頂部內邊距的距離 |
android:paddingBottom | 設置布局頂部內邊距的距離 |
android:paddingLeft | 設置布局頂部內邊距的距離 |
android:paddingRight | 設置布局頂部內邊距的距離 |
android:paddingpadding | 設置布局頂部內邊距的距離 |
例如設置btn_center按鈕的頂部內邊距為10dp,底部內邊距為0dp
<?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">
<Button
android:id="@+id/btn_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center"
android:layout_centerInParent="true"
android:paddingTop="10dp"
android:paddingBottom="0dp"/>
<Button
android:id="@+id/btn_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="right"
android:layout_toRightOf="@id/btn_center"
android:layout_alignBaseline="@id/btn_center"
android:layout_marginLeft="20dp"
/>
</RelativeLayout>