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

Android 佈局詳解:TableLayout 表格佈局的行列規則與三大列屬性

編輯:關於 Android 開發

TableLayout 表格佈局入門

不論是 Java Swing 還是 HTML,表格都是表達結構化資料的常見手法,Android 也提供了對應的容器:TableLayout。它把子元素視為表格中的儲存格,適合呈現鍵盤、表單、對齊的多欄資料。

行數與列數是怎麼算出來的

TableLayout 並不需要你明確宣告行列數,規則相當直觀:

  • 直接放進 TableLayout 的控制項,會獨佔一整列。
  • 如果想在同一列放多個控制項,就要把它們包進 TableRow 裡。
  • TableRow 裡有幾個子元素,這一列就有幾欄。

三個常用的欄位控制屬性

這三個屬性的索引都從 0 開始,而且一次可以指定多欄,用逗號分隔。

  • shrinkColumns(可收縮):被指定的欄位允許壓縮寬度,讓整個表格塞得進父容器的可用寬度,避免溢出。
  • stretchColumns(可拉伸):被指定的欄位會撐開,把表格剩下的空白空間吃滿,讓內容填齊。
  • collapseColumns(可隱藏):被指定的欄位直接收攏為零寬,等同隱藏,常用於依狀態切換顯示的欄位。

實作範例

以下範例刻意不為控制項設定 ID,只為了把三種屬性的視覺差異示範清楚。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- 第一個表格:第二欄可收縮,第三欄可拉伸 -->
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="1"
        android:stretchColumns="2">

        <!-- 直接加入的元件會獨佔一列 -->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="獨自占一列" />

        <TableRow>
            <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="收縮按鈕" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拉伸按鈕" />
        </TableRow>
    </TableLayout>

    <!-- 第二個表格:指定第二欄隱藏 -->
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:collapseColumns="1">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="獨自占一列" />

        <TableRow>
            <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="普通按鈕" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="普通按鈕" />
        </TableRow>
    </TableLayout>

    <!-- 第三個表格:第二、三欄都可拉伸 -->
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1,2">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="獨自占一列" />

        <TableRow>
            <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="拉伸按鈕" />
            <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拉伸按鈕" />
        </TableRow>
    </TableLayout>
</LinearLayout>

什麼時候該用 TableLayout

表格佈局的優勢在「對齊」:同一欄的元件會自動對齊寬度,做起來的小鍵盤、報表、設定清單都很省事。但如果欄位內容長度差異極大,拉伸與收縮的組合會讓版面難以預期,這時改用線性佈局加上權重,往往更好控制。

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