編輯:關於 Android 開發

GridLayout 是 Android 4.0(API 14)之後才加入的佈局管理器,用 row 與 column 的概念把畫面切成表格,再讓元件跨列或跨欄填進去。使用上有兩個前提:
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>
其一,rowCount 與 columnCount 建議明確寫出,否則元件會依序自動填格,跨欄時容易錯位。其二,跨度屬性寫在子元件的 layout_columnSpan 上,而不是容器上。其三,若要在舊版系統使用,記得把 GridLayout 換成 v7 支援庫的類別,並在 gradle 中加入 appcompat-v7 依賴。
Android 熱修復實戰:把補丁注入 ClassLoader 並解決 CLASS_ISPREVERIFIED
要解決的兩個問題 上一篇從 dex 分包原理出發,說明了熱修復為什麼行得通。這一篇動手做:一是怎麼把修好的類打包成 dex,二是怎麼把外部的 dex 插進 ClassLo
Android 分享功能實作:整合系統分享與第三方分享的可擴充架構
Android 分享功能實作:整合系統分享與第三方分享的可擴充架構 現在幾乎每個應用都會內建分享功能,通常包含系統分享(郵件、簡訊等)與第三方分享(QQ、微信等)。有些團
在 Android 中生成 Excel 檔案並儲存到本機
在 Android 中生成 Excel 有些專案會需要在客戶端直接產生 Excel 檔案,再提供給使用者下載或寄送。本文以訂單資訊為例,說明如何在 Android 中建立
深入理解 Activity 啟動流程:從 Launcher 點擊到生命周期執行的源碼追蹤
深入理解 Activity 啟動流程:從 Launcher 點擊到生命周期執行的源碼追蹤 一、概要交互流程 使用者在 Launcher 程式點擊應用圖示,就能啟動應用的