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

Android 佈局詳解:GridLayout 網格佈局與計算器實作

編輯:關於 Android 開發

GridLayout 網格佈局

GridLayout 是 Android 4.0(API 14)之後才加入的佈局管理器,用 row 與 column 的概念把畫面切成表格,再讓元件跨列或跨欄填進去。使用上有兩個前提:

  • 在 Android 4.0 以上的版本可以直接使用 android.widget.GridLayout。
  • 若要相容更舊的平台,則需引入支援庫,寫成 <android.support.v7.widget.GridLayout>。

相比 TableLayout,GridLayout 的好處是不必為每一列額外包一層 TableRow,元件依序填入即可,並可透過 layout_rowSpan 與 layout_columnSpan 輕鬆做出合併儲存格的效果。

案例:打造一個計算機介面

下面以 6 列 4 欄的網格排出計算機按鍵。顯示區與清除鍵各自橫跨 4 欄,其餘按鍵依序填入;layout_gravity="fill" 讓元件撐滿所屬格位。

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rowCount="6"
    android:columnCount="4"
    android:layout_gravity="fill">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="80sp"
        android:layout_marginRight="5dp"
        android:layout_marginLeft="5dp"
        android:layout_columnSpan="4"
        android:background="#eee"
        android:padding="3sp"
        android:textColor="#000" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:text="清除"
        android:textColor="#000"
        android:textSize="24dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:background="@android:color/background_light" />

    <Button android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="7" android:textColor="#000" android:textSize="24dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="@android:color/background_light" />
    <Button android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="8" android:textColor="#000" android:textSize="24dp" android:layout_marginLeft="10dp" android:background="@android:color/background_light" />
    <Button android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="9" android:textColor="#000" android:textSize="24dp" android:layout_marginLeft="10dp" android:background="@android:color/background_light" />
    <Button android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:textColor="#000" android:textSize="24dp" android:layout_marginLeft="10dp" android:background="@android:color/background_light" />

    <Button android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4" android:textColor="#000" android:textSize="24dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="@android:color/background_light" />
    <Button android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5" android:textColor="#000" android:textSize="24dp" android:layout_marginLeft="10dp" android:background="@android:color/background_light" />
    <Button android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="6" android:textColor="#000" android:textSize="24dp" android:layout_marginLeft="10dp" android:background="@android:color/background_light" />
    <Button android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:textColor="#000" android:textSize="24dp" android:layout_marginLeft="10dp" android:background="@android:color/background_light" />

    <Button android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:textColor="#000" android:textSize="24dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="@android:color/background_light" />
    <Button android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" android:textColor="#000" android:textSize="24dp" android:layout_marginLeft="10dp" android:background="@android:color/background_light" />
    <Button android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" android:textColor="#000" android:textSize="24dp" android:layout_marginLeft="10dp" android:background="@android:color/background_light" />
    <Button android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="*" android:textColor="#000" android:textSize="24dp" android:layout_marginLeft="10dp" android:background="@android:color/background_light" />

    <Button android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="." android:textColor="#000" android:textSize="24dp" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:background="@android:color/background_light" />
    <Button android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:textColor="#000" android:textSize="24dp" android:layout_marginLeft="10dp" android:background="@android:color/background_light" />
    <Button android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="=" android:textColor="#000" android:textSize="24dp" android:layout_marginLeft="10dp" android:background="@android:color/background_light" />
    <Button android:layout_marginTop="20dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/" android:textColor="#000" android:textSize="24dp" android:layout_marginLeft="10dp" android:background="@android:color/background_light" />
</GridLayout>

使用 GridLayout 的三個實務提醒

其一,rowCount 與 columnCount 建議明確寫出,否則元件會依序自動填格,跨欄時容易錯位。其二,跨度屬性寫在子元件的 layout_columnSpan 上,而不是容器上。其三,若要在舊版系統使用,記得把 GridLayout 換成 v7 支援庫的類別,並在 gradle 中加入 appcompat-v7 依賴。

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