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

Android 常用布局控件詳解:RelativeLayout 相對佈局

編輯:Android 開發教學

LinearLayout 與 TableLayout 已能滿足大部分介面需求,但有時候會不夠靈活。這時可以考慮 RelativeLayout。

核心概念:相對定位

RelativeLayout 是一種相對佈局容器,其內部的子元素可以用「彼此之間的相對位置」或「與容器的相對位置」來定位,概念上有點像網頁設計中的 CSS。

指定控件位置時,你要說明它與其他控件的相對關係——在另一個控件的左邊、右邊、上方、下方,或與某個邊緣對齊等等。一個控件可以同時指定與多個其他控件的相對位置,這讓設計層次豐富的介面變得相當方便。

屬性速查

// 相對於給定 ID 的控件
android:layout_above       把該控件的底部置於給定 ID 控件之上
android:layout_below       把該控件的底部置於給定 ID 控件之下
android:layout_toLeftOf    該控件右邊緣與給定 ID 左邊緣對齊
android:layout_toRightOf   該控件左邊緣與給定 ID 右邊緣對齊
android:layout_alignBaseline  與給定 ID 的 baseline 對齊
android:layout_alignTop    頂部邊緣與給定 ID 頂部對齊
android:layout_alignBottom 底部邊緣與給定 ID 底部對齊
android:layout_alignLeft   左邊緣與給定 ID 左邊緣對齊
android:layout_alignRight  右邊緣與給定 ID 右邊緣對齊

// 相對於父容器
android:layout_alignParentTop     頂部與父容器頂部對齊
android:layout_alignParentBottom  底部與父容器底部對齊
android:layout_alignParentLeft    左部與父容器左部對齊
android:layout_alignParentRight   右部與父容器右部對齊

// 居中
android:layout_centerHorizontal   水平居中
android:layout_centerVertical     垂直居中
android:layout_centerInParent     置於父容器正中央

// 邊距偏移(px)
android:layout_marginTop / Bottom / Left / Right

範例

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10px" >

    <TextView
        android:id="@+id/textView"
        android:text="TextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <!-- 沒有做位置設定,預設在容器的左上角 -->

    <EditText
        android:id="@+id/editText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView"
        android:background="@android:drawable/editbox_background" />
    <!-- editText 位於 textView 下方 -->

    <Button
        android:id="@+id/buttonSure"
        android:text="確定"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/editText"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="10px" />
    <!-- 在 editText 下方,並相對於父容器靠右對齊。
         marginLeft 讓它與右側其他內容間隔 10 像素 -->

    <Button
        android:id="@+id/buttonCancel"
        android:text="取消"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/buttonSure"
        android:layout_alignTop="@id/buttonSure" />
    <!-- 相對於 buttonSure 定位:右邊緣與 buttonSure 左邊緣對齊,
         頂部與 buttonSure 頂部對齊 -->

</RelativeLayout>

兩個實務上的關鍵點

第一,被參照的 View 必須先宣告。RelativeLayout 是一次性解析的,若 layout_below="@id/xxx" 指向一個在它之後才定義的 View,系統會找不到參照目標而無效化該約束。範例中 TextView 寫在最前面正是這個原因。

第二,用 @id 而非 @+id 參照既有 View。@+id 是「建立這個 id」,@id 是「參照已存在的 id」。在 layout_below 這類約束屬性中應該用 @id,用 @+id 雖然通常也能運作,但語意上不精確。

今天該用它嗎

以現在的標準來看,RelativeLayout 已被 ConstraintLayout 取代。ConstraintLayout 表達能力更強(支援百分比、比例鏈、屏障、Guideline),而且能保持扁平層次,效能更好。Android Studio 的 Layout Editor 也預設使用它。

不過理解 RelativeLayout 仍有價值:ConstraintLayout 的約束概念與它一脉相承,學過相對定位再看 Constraint,幾乎是無縫接軌。維護舊專案時更是必備知識。

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