2.2.2相對(duì)布局

1 相對(duì)布局的概念

相對(duì)布局是通過(guò)相對(duì)定位的方式制定控件位置,即以其他控件或父容器為參照物擺放控件位置,在設(shè)計(jì)相對(duì)布局時(shí),要遵循控件之間的依賴關(guān)系。

相對(duì)布局以一對(duì)<RelativeLayout></RelativeLayout>標(biāo)簽標(biāo)識(shí)。
每個(gè)控件都可以有一個(gè)id屬性,用來(lái)標(biāo)識(shí)自己的身份。
例如:為一個(gè)Button控件添加id
<Button id="@+id/btn_name/>

2 基本屬性


1 相對(duì)于父組件的位置,值為true 或 false

屬性 含義
android:layout_alignParentTop 將該控件的頂部與其父控件的頂部對(duì)齊;
android:layout_alignParentBottom 將該控件的底部與其父控件的底部對(duì)齊;
android:layout_alignParentLeft 將該控件的左部與其父控件的左部對(duì)齊;
android:layout_alignParentRight 將該控件的右部與其父控件的右部對(duì)齊;
android:layout_centerHorizontal 將該控件置于水平居中;
android:layout_centerVertical 將該控件置于垂直居中;
android:layout_centerInParent 將該控件置于父控件的中央;

例如:設(shè)置按鈕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 相對(duì)于給定ID控件

屬性 含義
android:layout_above 將該控件的底部置于給定ID的控件之上;
android:layout_below 將該控件的底部置于給定ID的控件之下;
android:layout_toLeftOf 將該控件的右邊緣與給定ID的控件左邊緣對(duì)齊;
android:layout_toRightOf 將該控件的左邊緣與給定ID的控件右邊緣對(duì)齊;
android:layout_alignBaseline 將該控件的baseline與給定ID的baseline對(duì)齊;
android:layout_alignTop 將該控件的頂部邊緣與給定ID的頂部邊緣對(duì)齊;
android:layout_alignBottom 將該控件的底部邊緣與給定ID的底部邊緣對(duì)齊;
android:layout_alignLeft 將該控件的左邊緣與給定ID的左邊緣對(duì)齊;
android:layout_alignRight 將該控件的右邊緣與給定ID的右邊緣對(duì)齊;

例如:設(shè)定id為btn_right的按鈕處于btn_center按鈕的右側(cè),且與btn_center的基線對(duì)齊

<?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 相對(duì)控件之間的間距屬性

屬性 含義
android:layout_marginTop 上偏移的值;
android:layout_marginBottom 下偏移的值;
android:layout_marginLeft 左偏移的值;
android:layout_marginRight 右偏移的值;

例如:設(shè)定btn_right按鈕的左側(cè)邊距為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 設(shè)置控件的內(nèi)部間距

屬性 含義
android:paddingTop 設(shè)置布局頂部?jī)?nèi)邊距的距離
android:paddingBottom 設(shè)置布局頂部?jī)?nèi)邊距的距離
android:paddingLeft 設(shè)置布局頂部?jī)?nèi)邊距的距離
android:paddingRight 設(shè)置布局頂部?jī)?nèi)邊距的距離
android:paddingpadding 設(shè)置布局頂部?jī)?nèi)邊距的距離

例如設(shè)置btn_center按鈕的頂部?jī)?nèi)邊距為10dp,底部?jī)?nèi)邊距為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>   
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 6,547評(píng)論 0 17
  • 問(wèn)答題47 /72 常見瀏覽器兼容性問(wèn)題與解決方案? 參考答案 (1)瀏覽器兼容問(wèn)題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 13,809評(píng)論 1 92
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,466評(píng)論 25 708
  • 看了幾篇文章以及自己寫了一些,關(guān)于布局的問(wèn)題,根據(jù)別人的寫作總結(jié)一下得到 一.基本理論Android六大基本布局分...
    shuaikun閱讀 837評(píng)論 0 4
  • 纖月黃昏院,輕暈人醉淺。 籠花嬌欲泣,憶久只自憐。 青絲待霜白,紅妝換舊顏。 玉枕淚痕泫,情深無(wú)由怨。
    時(shí)影書閱讀 287評(píng)論 0 6