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

Android 開發重點難點(1):RelativeLayout 相對布局詳解

編輯:關於 Android 開發

前言

之前的「Android 開發」系列以完成實驗為主線,一次會混進不少知識點,讀起來難免有點雜。因此另開了這個「重點難點」系列,把實驗過程中真正容易卡住、容易出錯的地方單獨拎出來講清楚。實驗系列也會持續更新。

系列就從布局開始。LinearLayout 大家好理解,這篇專講 RelativeLayout。

核心概念:一切都靠參照

和 LinearLayout 一樣,RelativeLayout 是實務上最常出現的布局之一。「相對」二字意味著需要參照物——以某個兄弟組件或父容器為基準來定位。這裡說的兄弟組件,指的是同一個布局檔內的組件;如果去參照另一個布局裡的組件,就會出錯。

把 LinearLayout 的 weight 權重屬性與 RelativeLayout 搭配好,多數不同解析度的自適應問題都能解決。

舉個例子:小明在上學路上,他的位置可以用「離家多少公尺」或「離學校多少公尺」來描述,差別只在參照物不同。RelativeLayout 的定位邏輯正是如此。

一、容器層級的對齊設定

android:gravity:設定容器內各子組件的對齊方式。

android:ignoreGravity:設了這個屬性的組件,不受 gravity 影響。

二、以父容器為基準定位

想貼哪一邊,就把對應屬性設為 true:

layout_alighParentLeft 左對齊;layout_alighParentRight 右對齊;layout_alighParentTop 頂端對齊;layout_alighParentBottom 底部對齊。

layout_centerHorizontal 水平居中;layout_centerVertical 垂直居中;layout_centerInParent 置於正中。

三、以兄弟組件為基準定位

這一類屬性的值要寫兄弟組件的 id:

layout_toLeftOf 放在其左邊;layout_toRightOf 放在其右邊;layout_above 放在其上方;layout_below 放在其下方。

layout_alignTop 上邊界對齊;layout_alignBottom 下邊界對齊;layout_alignLeft 左邊界對齊;layout_alignRight 右邊界對齊。

用這些屬性可以拼出很經典的梅花布局:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- 這個是在容器中央的 -->
    <ImageView
        android:id="@+id/img1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_centerInParent="true"
        android:src="@drawable/pic1" />

    <!-- 在中間圖片的左邊 -->
    <ImageView
        android:id="@+id/img2"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_toLeftOf="@id/img1"
        android:layout_centerVertical="true"
        android:src="@drawable/pic2" />

    <!-- 在中間圖片的右邊 -->
    <ImageView
        android:id="@+id/img3"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_toRightOf="@id/img1"
        android:layout_centerVertical="true"
        android:src="@drawable/pic3" />

    <!-- 在中間圖片的上面 -->
    <ImageView
        android:id="@+id/img4"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_above="@id/img1"
        android:layout_centerHorizontal="true"
        android:src="@drawable/pic4" />

    <!-- 在中間圖片的下面 -->
    <ImageView
        android:id="@+id/img5"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_below="@id/img1"
        android:layout_centerHorizontal="true"
        android:src="@drawable/pic5" />

</RelativeLayout>

四、Margin 與 Padding 的差別

這兩個最容易搞混,記法很簡單:margin 是「外」——組件與父容器之間的距離;padding 是「內」——組件內部內容與邊框之間的距離。

Margin:組件與父容器的邊距

layout_margin 四周外距;layout_marginLeft 左外距;layout_marginTop 上外距;layout_marginRight 右外距;layout_marginBottom 下外距。

Padding:組件內部元素之間的邊距

padding 四周內距;paddingLeft 左內距;paddingTop 上內距;paddingRight 右內距;paddingBottom 下內距。

兩者後面都接一個數值,通常用 dp,例如 android:layout_margin = "10dp"。

效果對比如下:

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/btn1"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Button" />

    <Button
        android:paddingLeft="100dp"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Button"
        android:layout_toRightOf="@id/btn1" />

</RelativeLayout>

第二個按鈕設了 paddingLeft = 100dp,結果按鈕被撐寬了 100dp,因為內部內容被推開;若改成 marginLeft = 100dp,則整個按鈕會向右平移 100dp。一個變胖,一個搬家,這就是兩者的本質差異。

總結:屬性速查

第一類:值為 true 或 false

layout_centerHorizontal 水平居中;layout_centerVertical 垂直居中;layout_centerInparent 相對於父元素完全居中;layout_alignParentBottom 貼緊父元素下緣;layout_alignParentLeft 貼緊父元素左緣;layout_alignParentRight 貼緊父元素右緣;layout_alignParentTop 貼緊父元素上緣;layout_alignWithParentIfMissing 若對應的兄弟元素找不到,就以父元素為參照。

第二類:值必須是 id 參照,寫成 @id/id-name

layout_below 在某元素下方;layout_above 在某元素上方;layout_toLeftOf 在某元素左邊;layout_toRightOf 在某元素右邊;layout_alignTop 上緣與某元素對齊;layout_alignLeft 左緣對齊;layout_alignBottom 下緣對齊;layout_alignRight 右緣對齊。

第三類:值為具體尺寸,如 30dip、40px

layout_marginBottom 離某元素底緣的距離;layout_marginLeft 離某元素左緣的距離;layout_marginRight 離某元素右緣的距離;layout_marginTop 離某元素上緣的距離。

其他常用屬性

android:hint(EditText):輸入框為空時顯示的提示文字。

android:gravity:約束 View「內容」的位置。以 Button 為例,gravity="right" 會讓按鈕上的文字靠右。

android:layout_gravity:設定 View 相對於「父 View」的位置。同样以 Button 為例,layout_gravity="right" 會讓整個按鈕靠右。這兩者的差異值得反覆確認,面試也常考。

android:layout_alignParentRight:讓當前控件右緣與父控件右緣對齊,值只能是 true 或 false,預設 false。

android:scaleType:控制圖片如何縮放與移動以配合 ImageView 的尺寸,取值含 center、centerCrop、centerInside、fitCenter、fitEnd、fitStart、fitXY、matrix。其中 fitXY 會不按比例拉伸,容易變形;centerCrop 會填滿但可能裁切,是列表縮圖最常用的選擇。

最後補一個小提醒:drawable 目錄裡的圖片檔名不能用大寫字母。

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