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

Android 布局完全解析:LinearLayout、RelativeLayout 與 TableLayout

編輯:關於 Android 程式設計

一個 Android 畫面上往往有許多控制元件,那麼該如何安排它們的位置與排列方式呢?這就必須依靠「容器(Layout)」來存放並排列這些元件,其角色就像 HTML 中的 div 與 table。本文系統性地介紹 Android 的幾種主要布局。

Android 常見的布局有:LinearLayout、RelativeLayout、TableLayout 與 AbsoluteLayout。其中 AbsoluteLayout 需靠指定 x/y 座標定位,缺乏彈性,已不建議使用,因此本文著重介紹前三種。

一、LinearLayout(線性布局)

包含在 LinearLayout 中的元件會依序排成一列或一行,概念上類似 Swing 的 FlowLayout。它的常用屬性包括:

  • Orientation(方向):決定排列是 horizontal 還是 vertical,例如 android:orientation="vertical",也可用 setOrientation() 在程式碼中設定。
  • Fill Mode(填充方式):透過 layout_width 與 layout_height 指定,可取具體像素值、wrap_content(依內容實際長度)或 fill_parent(填滿可用空間)。
  • Weight(權重):想讓同一行或列的元件按比例分配空間時非常有用。例如希望某元件佔另一元件的兩倍寬度,可將兩者的 layout_weight 分別設為 2 與 1。

以下範例展示一個巢狀的線性布局:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <!-- 橫向排列 -->
    <LinearLayout android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1">
        <TextView android:text="red"    android:background="#aa0000"
            android:layout_width="wrap_content" android:layout_height="fill_parent"
            android:layout_weight="1" />
        <TextView android:text="green"  android:background="#00aa00"
            android:layout_width="wrap_content" android:layout_height="fill_parent"
            android:layout_weight="1" />
        <TextView android:text="blue"   android:background="#0000aa"
            android:layout_width="wrap_content" android:layout_height="fill_parent"
            android:layout_weight="1" />
    </LinearLayout>
</LinearLayout>

二、RelativeLayout(相對布局)

相對布局靠著「與父容器」或「與同一容器中其他元件」的相對位置來排列,彈性極高。常用屬性分兩類:

相對父容器:

  • android:layout_alignParentTop:頂部與父容器頂部對齊,另有 alignParentBottom、alignParentLeft、alignParentRight。

相對其他元件:

  • android:layout_above:位於某元件上方,另有 layout_below、layout_toLeftOf、layout_toRightOf。
  • android:layout_alignTop:與某元件頂部對齊,另有 layout_alignBottom、layout_alignLeft、layout_alignRight。

既然是「相對」於某元件,就必須指名是哪一個。例如元件 A 的 id 為 @+id/widget_a,若 B 要排在 A 下方,就寫 android:layout_below="@id/widget_a"。

<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: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:text="OK" />
    <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>

三、TableLayout(表格布局)

表格布局類似 HTML 的 Table,以 TableRow 定義每一列。若某元件要跨越多列,可設定 android:layout_span(類似 HTML 的 colspan);預設元件會依序落在每一欄,也可用 android:layout_column 指定欄位。若某欄內容過長或過短,可用 android:stretchColumns 與 android:shrinkColumns 來撐開或縮窄該欄。

<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..."
            android:padding="3dip" />
        <TextView android:text="Ctrl-O" android:gravity="right"
            android:padding="3dip" />
    </TableRow>
    <TableRow>
        <TextView android:layout_column="1" android:text="Save..."
            android:padding="3dip" />
        <TextView android:text="Ctrl-S" android:gravity="right"
            android:padding="3dip" />
    </TableRow>
</TableLayout>

在上例中透過 android:stretchColumns="1" 把第二欄撐開,讓版面更平均。

該怎麼選?

實務上:LinearLayout 適合簡單的直線排列;RelativeLayout 彈性最高、能減少不必要的巢狀,適合較複雜的畫面;TableLayout 則專精於表格式的資料呈現。熟悉這三者,幾乎就能應付絕大多數的 Android 畫面配置需求。

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