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

Android 介面設計:四種常用布局的實際對照

編輯:關於 Android 程式設計

布局是 Android 介面的骨架,選對容器能省下大量巢狀與效能調校的工夫。以下用四個可運行的小例子,把 LinearLayout、RelativeLayout、GridLayout 與 TableLayout 的特性直接呈現出來。

一、線性布局 LinearLayout

最直覺的容器,用 orientation 決定排列方向,用 layout_weight 分配剩餘空間。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <Button android:id="@+id/btn1" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="1" />
        <Button android:id="@+id/btn2" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="2" />
        <Button android:id="@+id/btn3" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="3" />
        <Button android:id="@+id/btn4" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="4" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <Button android:id="@+id/btn5" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="4" />
        <Button android:id="@+id/btn6" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:text="5" />
        <Button android:id="@+id/btn7" android:layout_width="wrap_content"
                android:layout_height="wrap_content" android:layout_weight="1"
                android:text="6" />
    </LinearLayout>
</LinearLayout>

結果:

注意第二組的第三顆按鈕設了 weight=1,因此它會佔滿該列剩下的寬度。這是 LinearLayout 最實用的特性:不需要寫死 pixel 數值就能做等分排列。

二、相對布局 RelativeLayout

允許以「相對於父容器」或「相對於其他元件」的方式定位,能有效減少巢狀層數。

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    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" >

    <Button android:id="@+id/bbtn1" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="中央"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" />

    <Button android:id="@+id/bbtn2" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="左上角"
            android:layout_above="@id/bbtn1" android:layout_toLeftOf="@id/bbtn1" />

    <Button android:id="@+id/bbtn3" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="左下角"
            android:layout_below="@id/bbtn1" android:layout_toLeftOf="@id/bbtn1" />

    <Button android:id="@+id/bbtn4" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="右下"
            android:layout_below="@id/bbtn1" android:layout_toRightOf="@id/bbtn1" />

    <Button android:id="@+id/bbtn5" android:layout_width="wrap_content"
            android:layout_height="wrap_content" android:text="右上"
            android:layout_above="@id/bbtn1" android:layout_toRightOf="@id/bbtn1" />
</RelativeLayout>

結果:

這裡有個實務上很重要的規則:被參考的元件必須先出現在配置檔中(或在程式中先建立),否則 @id/bbtn1 會找不到而導致定位失效。另外,RelativeLayout 的效能與巢狀深度及參考官索的複雜度有關,層級太深時配置時間會明顯變長。

三、格狀布局 GridLayout

以列與行構成網格,支援 columnSpan 與 rowSpan 做跨格合併,很適合做出計算機那類的鍵盤版面。

<GridLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:rowCount="5"
    android:columnCount="4" >

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="7" />
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="8" />
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="9" />
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="÷" />

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="4" />
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="5" />
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="6" />
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="×" />

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="=" android:layout_rowSpan="2"
            android:layout_gravity="fill" />
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:text="+" android:layout_columnSpan="2"
            android:layout_gravity="fill" />
</GridLayout>

結果:

這裡的 layout_gravity="fill" 是必要的——單純設定 rowSpan 或 columnSpan 只會讓元件佔據更多格子,若不搭配 gravity,元件本身仍會維持原本的大小而不會被拉滿。

四、TableLayout 與 TableRow

TableLayout 實際上是 LinearLayout 的擴充,每一列用 TableRow 表示。它的特色是可以針對「特定欄」設定伸縮行為。

<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="1"
    android:shrinkColumns="2"
    android:padding="3dip" >

    <TableRow>
        <TextView android:text="名稱" />
        <TextView android:text="數值" />
    </TableRow>

    <TableRow>
        <TextView android:text="很長的一段文字內容用來示範收縮行為" />
        <Button android:text="確認" />
    </TableRow>
</TableLayout>

結果:

stretchColumns 指定哪些欄可以伸展填滿剩餘空間,shrinkColumns 則指定哪些欄在空間不足時可以被壓縮。這兩個屬性是 TableLayout 相對於 GridLayout 的主要差異,也是它在處理「欄寬需要依內容調整」的表格時仍有價值的原因。

怎麼選

實務上的判斷可以很簡單:內容是清單就別用這些容器,直接上 ListView 或 RecyclerView;需要等分排列用 LinearLayout + weight;需要重疊或少量相對定位用 RelativeLayout(或 ConstraintLayout);規則的網格用 GridLayout;欄寬需依內容伸縮的表格用 TableLayout。

最後補充一點:現在新專案幾乎都會改用 ConstraintLayout。它能用扁平的一層結構表達原本需要多層巢狀才能做到的版面,對效能與維護性都有明顯好處,上面這幾種布局則在讀懂舊專案時仍然必要。

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