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

RelativeLayout 詳解:一句話理清相對定位

編輯:關於 Android 程式設計

核心思想:一切都相對於別人

RelativeLayout 顧名思義,內部控制項的位置都是「相對」的。實踐中的套路是:先定好一個錨點控制項 A,其餘控制項紛紛與 A 建立相對關係,或者與已經和 A 建好關係的控制項建立關係。

這裡有一條血淚經驗:盡量不要在相對布局裡牽扯過多控制項的 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     本元素上邊緣與某元素上邊緣對齊
android:layout_alignLeft    本元素左邊緣與某元素左邊緣對齊
android:layout_alignBottom  本元素下邊緣與某元素下邊緣對齊
android:layout_alignRight   本元素右邊緣與某元素右邊緣對齊

第三類:具體尺寸屬性——值為 dip、px 等像素單位,描述與父容器的間距。

android:layout_marginBottom   距父容器底邊緣的距離
android:layout_marginLeft     距父容器左邊緣的距離
android:layout_marginRight    距父容器右邊緣的距離
android:layout_marginTop      距父容器上邊緣的距離

四句口訣記住全部關鍵字

屬性表看著多,其實拆開只有四種語義:

一、在誰的左邊或右邊 → toLeft-XX / toRight-XX

二、在誰的上面或下面 → X-below-X / X-above-X

三、和誰的邊緣對齊 → X-alignLeft_X 這一族

四、跟「爸爸」(父容器)打交道 → X_center_X / X_parent_X

把這四句在心裡默念幾遍,再聯想記憶,寫布局時基本不用查表。

最好的練習:用 Button 拼出「58」

相對布局說起來簡單,真正寫起來容易暈。我摸索出的最有效練習方式,就是用相對布局把數字拼成一個可辨識的圖形——比如用一堆 45dp 的 Button 擺出「58」這兩個字。這個練習會逼你把 above / below / toLeftOf / toRightOf / alignBottom / alignLeft 全部用上一遍。

下面是我拼出來的完整布局,仔細讀每一個 Button 的定位屬性,你會發現規律非常清晰:同一豎列的用 layout_above 串起來,需要並排時用 layout_toRightOf 搭配 layout_alignBottom 對齊底邊,轉折處則用 alignLeft 鎖住垂直對齊。

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <!-- 豎向串起左半邊 -->
    <Button android:id="@+id/one"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_centerVertical="true"/>
    <Button android:id="@+id/two"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_above="@id/one"/>
    <Button android:id="@+id/three"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_above="@id/two"/>
    <Button android:id="@+id/four"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_above="@id/three"/>

    <!-- 轉折:右移並底邊對齊 -->
    <Button android:id="@+id/five"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignBottom="@id/four"
        android:layout_toRightOf="@id/four"/>
    <Button android:id="@+id/six"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignBottom="@id/five"
        android:layout_toRightOf="@id/five"/>
    <Button android:id="@+id/seven"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignBottom="@id/one"
        android:layout_toRightOf="@id/one"/>
    <Button android:id="@+id/eight"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignBottom="@id/seven"
        android:layout_toRightOf="@id/seven"/>

    <!-- 下行:左邊緣對齊 + 位於下方 -->
    <Button android:id="@+id/nine"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignLeft="@id/eight"
        android:layout_below="@id/eight"/>
    <Button android:id="@+id/ten"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignLeft="@id/nine"
        android:layout_below="@id/nine"/>
    <Button android:id="@+id/eleven"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignLeft="@id/ten"
        android:layout_below="@id/ten"/>

    <!-- 向左折返 -->
    <Button android:id="@+id/twelve"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignBottom="@id/eleven"
        android:layout_toLeftOf="@id/eleven"/>
    <Button android:id="@+id/thirteen"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignBottom="@id/twelve"
        android:layout_toLeftOf="@id/twelve"/>

    <!-- 以 centerInParent 為錨點,拼出右半邊的 8 -->
    <Button android:id="@+id/fourteen"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_centerInParent="true"/>
    <Button android:id="@+id/fifteen"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_above="@id/fourteen"
        android:layout_alignLeft="@id/fourteen"/>
    <Button android:id="@+id/sixteen"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_above="@id/fifteen"
        android:layout_alignLeft="@id/fifteen"/>
    <Button android:id="@+id/seventeen"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_above="@id/sixteen"
        android:layout_alignLeft="@id/sixteen"/>
    <Button android:id="@+id/eighteen"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignBottom="@id/seventeen"
        android:layout_toRightOf="@id/seventeen"/>
    <Button android:id="@+id/nineteen"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignBottom="@id/eighteen"
        android:layout_toRightOf="@id/eighteen"/>
    <Button android:id="@+id/twenty"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignBottom="@id/nineteen"
        android:layout_toRightOf="@id/nineteen"/>

    <!-- 折返下行,opx 系列為「8」的下半圈 -->
    <Button android:id="@+id/oone"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignLeft="@id/twenty"
        android:layout_below="@id/twenty"/>
    <Button android:id="@+id/otwo"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignLeft="@id/oone"
        android:layout_below="@id/oone"/>
    <Button android:id="@+id/othree"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignLeft="@id/otwo"
        android:layout_below="@id/otwo"/>
    <Button android:id="@+id/ofour"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignTop="@id/othree"
        android:layout_toLeftOf="@id/othree"/>
    <Button android:id="@+id/ofive"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignTop="@id/ofour"
        android:layout_toLeftOf="@id/ofour"/>

    <!-- 以 fourteen 為基準,向下補滿中間豎列 -->
    <Button android:id="@+id/osix"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignLeft="@id/fourteen"
        android:layout_below="@id/fourteen"/>
    <Button android:id="@+id/oseven"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignLeft="@id/osix"
        android:layout_below="@id/osix"/>
    <Button android:id="@+id/oeight"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignLeft="@id/oseven"
        android:layout_below="@id/oseven"/>
    <Button android:id="@+id/onine"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignBottom="@id/oeight"
        android:layout_toRightOf="@id/oeight"/>
    <Button android:id="@+id/oten"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignBottom="@id/onine"
        android:layout_toRightOf="@id/onine"/>
    <Button android:id="@+id/oeleven"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_alignBottom="@id/oten"
        android:layout_toRightOf="@id/oten"/>
    <Button android:id="@+id/otwelve"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_above="@id/oeleven"
        android:layout_alignLeft="@id/oeleven"/>
    <Button android:id="@+id/othirteen"
        android:layout_width="45dp" android:layout_height="45dp"
        android:background="#e61212"
        android:layout_above="@id/otwelve"
        android:layout_alignLeft="@id/otwelve"/>
</RelativeLayout>

寫完這一段,你對 RelativeLayout 的理解基本就到位了。建議自己也動手拼一次,比看十篇文章都管用。

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