Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android UI 深入 RelativeLayout(相對佈局)完全解析

編輯:關於 Android 程式設計

相對佈局 RelativeLayout 入門

在 Android 開發裡,RelativeLayout 可以說是出鏡率最高的佈局容器之一,使用頻率甚至壓過了 LinearLayout。它最大的特點在於:子元件的位置既可以參照父容器本身,也可以參照同層的其他兄弟元件來確定。這種「你中有我」的定位方式帶來了極大的自由度,讓開發者能夠拼裝出層次豐富、視覺層次感更強的介面,同時也支援在 IDE 中透過拖拽完成佈局設計。

特別提醒:RelativeLayout 的子元件不支援設定 android:layout_gravity 屬性。

相比其他佈局,RelativeLayout 需要關注的屬性明顯更多,大致可以歸納為三大類。

一、android:ignoreGravity

對應方法:setIgnoreGravity(int)

這個屬性的作用是「豁免」——指定某些子元件不受父容器 android:gravity 的影響。RelativeLayout 同樣可以用 android:gravity 統一安排子元件的擺放位置,但實際開發中往往需要更細緻的控制,無法用單一規則滿足所有元件,於是便透過這個屬性對個別元件做特殊處理。

舉個例子:

<RelativeLayout android:gravity="bottom" android:ignoreGravity="@id/view1" android:layout_height="fill_parent" android:layout_width="fill_parent">
    <ImageView android:id="@+id/view1" android:layout_height="100dp" android:layout_width="100dp" android:scaleType="fitXY" android:src="@drawable/muogu">
        <ImageView android:layout_height="100dp" android:layout_width="100dp" android:scaleType="fitXY" android:src="@drawable/nailiu16">
        </ImageView>
    </ImageView>
</RelativeLayout>

二、只能設定為 true / false 的 XML 屬性

這類屬性用來彌補「子元件無法設定 layout_gravity」的遺憾,可以直接指定元件相對父容器的擺放方式。屬性表如下:

屬性名 功能說明 android:layout_centerHorizontal 讓該元件相對父容器水平居中 android:layout_centerVertical 讓該元件相對父容器垂直居中 android:layout_centerInParent 讓該元件相對父容器居中 android:layout_alignParentBottom 讓該元件與父容器底端對齊 android:layout_alignParentTop 讓該元件與父容器頂端對齊 android:layout_alignParentLeft 讓該元件與父容器左端對齊 android:layout_alignParentRight 讓該元件與父容器右端對齊 android:layout_alignParentEnd 讓該元件與父容器結束端對齊(右上角) android:layout_alignParentStart 讓該元件與父容器起始端對齊(左上角)

需要注意的是,這些屬性可以組合使用,但若同時啟用彼此衝突的屬性(例如 layout_alignParentLeft 與 layout_alignParentRight 並存),就會產生相當怪異的效果,因此不建議同時使用互相矛盾的屬性。

<RelativeLayout android:layout_height="fill_parent" android:layout_width="fill_parent">
    <ImageView android:id="@+id/view1" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:layout_height="100dp" android:layout_width="100dp" android:scaleType="fitXY" android:src="@drawable/muogu">
    </ImageView>
</RelativeLayout>

系統會盡力去滿足所有屬性,結果元件被拉伸成四不像的怪模樣。

三、只能設定為其他 UI 元件 ID 的 XML 屬性

這才是 RelativeLayout 真正的核心屬性。透過它們,各子元件之間才能建立起相對位置關係。屬性表如下:

屬性名 功能說明 android:layout_toRightOf 讓該元件位於指定 ID 元件的右側 android:layout_toLeftOf 讓該元件位於指定 ID 元件的左側 android:layout_above 讓該元件位於指定 ID 元件的上方 android:layout_below 讓該元件位於指定 ID 元件的下方 android:layout_alignTop 讓該元件頂端與指定 ID 元件的上邊界對齊 android:layout_alignBottom 讓該元件底端與指定 ID 元件的下邊界對齊 android:layout_alignLeft 讓該元件左端與指定 ID 元件的左邊界對齊 android:layout_alignRight 讓該元件右端與指定 ID 元件的右邊界對齊 android:layout_alignStart 讓該元件起始位置與指定 ID 元件的起始位置對齊(左上角) android:layout_alignEnd 讓該元件結束位置與指定 ID 元件的結束位置對齊(右上角)

此外,還可以搭配 margin 或 padding 對元件做細微的位置微調,示例如下:

<RelativeLayout android:layout_height="fill_parent" android:layout_width="fill_parent">
    <ImageView android:id="@+id/view1" android:layout_alignParentEnd="true" android:layout_height="100dp" android:layout_width="100dp" android:scaleType="fitXY" android:src="@drawable/nailiu16">
        <ImageView android:id="@+id/view2" android:layout_alignParentRight="true" android:layout_below="@+id/view1" android:layout_height="100dp" android:layout_width="100dp" android:scaleType="fitXY" android:src="@drawable/muogu">
            <ImageView android:layout_alignBottom="@+id/view2" android:layout_alignParentRight="true" android:layout_height="100dp" android:layout_margin="50dp" android:layout_width="100dp" android:scaleType="fitXY" android:src="@drawable/muogu">
            </ImageView>
        </ImageView>
    </ImageView>
</RelativeLayout>

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved