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

Android RelativeLayout 屬性大全(中文逐項說明)

編輯:關於 Android 程式設計

RelativeLayout 是 Android 裡最靈活的配置之一,靠著「相對於誰」來定位元件。屬性雖多,但依參照對象分類之後就好記了。

一、相對於某個指定 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 的右邊緣對齊

二、相對於父容器

android:layout_alignParentTop 為 true 時,本元件頂部與父容器頂部對齊
android:layout_alignParentBottom 為 true 時,本元件底部與父容器底部對齊
android:layout_alignParentLeft 為 true 時,本元件左側與父容器左側對齊
android:layout_alignParentRight 為 true 時,本元件右側與父容器右側對齊

三、置中

android:layout_centerHorizontal 為 true 時水平置中
android:layout_centerVertical 為 true 時垂直置中
android:layout_centerInParent 為 true 時置於父容器正中央

四、指定位移像素

android:layout_marginTop 向上偏移
android:layout_marginBottom 向下偏移
android:layout_marginLeft 向左偏移
android:layout_marginRight 向右偏移

實際範例

android:layout_below = "@id/***"
android:layout_alignBaseline = "@id/***"
android:layout_alignParentTop = true
android:layout_marginLeft = "10px"
<?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">

    <TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Type here:" />

    <EditText
        android:id="@+id/entry"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/editbox_background"
        android:layout_below="@id/label">

        <Button
            android:id="@+id/ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/entry"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="10dip"
            android:text="OK" />
    </EditText>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@id/ok"
        android:layout_alignTop="@id/ok"
        android:text="Cancel" />
</RelativeLayout>

逐行解讀

android:layout_below="@id/label" 把目前元件放在 id 為 label 的元件下方。
android:layout_alignParentRight="true" 讓目前元件的右端與父容器右端對齊;此屬性值只能是 true 或 false,預設 false。
android:layout_marginLeft="10dip" 讓目前元件左側留出指定間距。
android:layout_toLeftOf="@id/ok" 把目前元件放在 id 為 ok 的元件左邊。
android:layout_alignTop="@id/ok" 讓目前元件與 id 元件的頂端對齊。

依屬性值型別快速歸納

第一類:值為 true 或 false
android:layout_centerHorizontal
android:layout_centerVertical
android:layout_centerInParent
android:layout_alignParentBottom
android:layout_alignParentLeft
android:layout_alignParentRight
android:layout_alignParentTop
android:layout_alignWithParentIfMissing

第二類:值必須是 id 引用,寫成 "@id/id-name"
android:layout_below
android:layout_above
android:layout_toLeftOf
android:layout_toRightOf
android:layout_alignTop(以及 alignBottom / alignLeft / alignRight / alignBaseline)

第三類:值為具體尺寸,如 30dip、40px
android:layout_marginBottom
android:layout_marginLeft
android:layout_marginRight
android:layout_marginTop

實務提醒

使用 RelativeLayout 時務必注意參照順序:被參照的元件若宣告在後面,會導致對齊失效。另一個常見地雷是「互相參照」——A 說自己在 B 下方,B 又說自己在 A 下方,這會讓配置計算陷入循環,系統會直接拋出錯誤。避開這兩點,RelativeLayout 的層級壓縮能力其實優於多層 LinearLayout 嵌套。

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