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

深入 Android 五大佈局物件: from FrameLayout 到 TableLayout

編輯:關於 Android 程式設計

佈局是 Android UI 開發的骨架。Android 提供了五種基礎佈局——FrameLayout(框架佈局)、LinearLayout(線性佈局)、AbsoluteLayout(絕對佈局)、RelativeLayout(相對佈局)與 TableLayout(表格佈局),各自適用於不同的排版需求。本文逐一剖析它們的特性與用法。

FrameLayout:最簡單的堆疊容器

FrameLayout 是最簡單的佈局物件,它被設計成螢幕上一塊空白的備用區域,用來容納單一物件(例如一張圖片)。所有子元素都會固定在螢幕左上角,且無法為子元素指定位置;後加入的子元素會直接覆蓋在前者之上,將其部分或全部遮住(除非後者是透明的)。右圖這種「兩張圖片疊放、後者覆蓋前者」的效果,常見於視頻點播的封面與播放鈕組合。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <Button
        android:text="button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:text="textview"
        android:textColor="#0000ff"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</FrameLayout>

LinearLayout:靈活的線性排列

LinearLayout 會依照你設定的垂直或水平屬性,把子元素依序堆疊。垂直列表每行只有一個元素,水平列表則只有一行。它最大的亮點是支援為子元素指定 weight(權重),讓子元素能填滿螢幕的剩餘空間,避免一串小物件擠成一堆。

剩餘空間會按各子元素的 weight 比例分配,預設 weight 為 0。例如三個文字方塊中,若兩個指定 weight 為 1,這兩個就會等比例放大填滿剩餘空間,第三個則不放大。以下範例示範了嵌套 LinearLayout 與 weight 的搭配:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_weight="1" >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="hello,welcome to Livingstone's blog"
            android:textSize="15pt" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:layout_weight="2" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="2"
            android:background="#aa0000"
            android:gravity="center_horizontal"
            android:text="red" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#00aa00"
            android:gravity="center_horizontal"
            android:text="green" />
    </LinearLayout>
</LinearLayout>

AbsoluteLayout:精確座標定位

AbsoluteLayout 讓子元素指定精確的 x/y 座標值((0, 0) 為左上角,向右下移動座標變大)。它沒有頁邊框,允許元素互相重疊。但由於它讓界面過於剛性,在不同解析度的裝置上往往無法良好呈現,因此官方並不推薦使用,除非有正當理由。

RelativeLayout:以相對關係排版

RelativeLayout 允許子元素透過 ID 指定自己相對於其他元素或父元素的位置,例如靠右對齊、上下排列或置中。元素按順序排列,若第一個元素置中,其他元素便能以它為基準做相對定位。使用 XML 定義時,被關聯的元素必須先定義才能被引用。以下範例同樣展現了 LinearLayout 搭配 weight 的應用:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:layout_weight="1" >
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="hello,welcome to Livingstone's blog"
            android:textSize="15pt" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:layout_weight="2" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="2"
            android:background="#aa0000"
            android:gravity="center_horizontal"
            android:text="red" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="#00aa00"
            android:gravity="center_horizontal"
            android:text="green" />
    </LinearLayout>
</LinearLayout>

TableLayout:以行列組織資料

TableLayout 把子元素分配到列(row)或欄(column)中。它由多個 TableRow 組成,每個 TableRow 定義一個列。容器不會顯示 row、column 或 cell 的邊框線;每個 row 擁有 0 個或多個 cell,每個 cell 容納一個 View 物件。與 HTML 表格不同的是,Android 的 cell 不能跨欄。以下範例透過 android:stretchColumns 讓指定欄位自動伸展填滿:

<TableLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:stretchColumns="1">
    <TableRow>
        <TextView android:layout_column="1" android:text="Open..." />
        <TextView android:text="Ctrl-O" android:gravity="right" />
    </TableRow>
    <TableRow>
        <TextView android:layout_column="1" android:text="Save..." />
        <TextView android:text="Ctrl-S" android:gravity="right" />
    </TableRow>
    <View android:layout_height="2dip" android:background="#FF909090" /> <!-- 分隔線 -->
    <TableRow>
        <TextView android:text="X" />
        <TextView android:text="Export..." />
        <TextView android:text="Ctrl-E" android:gravity="right" />
    </TableRow>
    <TableRow>
        <TextView android:layout_column="1" android:text="Quit" android:padding="3dip" />
    </TableRow>
</TableLayout>

五種佈局各有擅場:FrameLayout 適合堆疊、LinearLayout 適合線性排列、RelativeLayout 適合靈活的相對定位、TableLayout 適合表格化資料,而 AbsoluteLayout 則因僵化而少用。理解它們的特性,才能在每個介面上選對工具。

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