編輯:Android 技術基礎
GridLayout 是 Android 4.0 之後加入的佈局管理器,用虛擬的細線把畫面切分成列、行與儲存格,還允許單一控件同時跨越多行多列。它的 API 和 LinearLayout 相當接近,只要改幾個標籤屬性就能上手。

1. 排列方向:和 LinearLayout 一樣可分水平與垂直,預設是水平,控件由左而右依序排列;透過 android:columnCount 指定列數後,控件會自動換行。GridLayout 中的子控件預設以 wrap_content 呈現。
2. 固定位置:要讓控件落在指定的列或行,設定 android:layout_row 與 android:layout_column。注意索引從 0 開始,layout_row="0" 代表第一行、layout_column="0" 代表第一列,和一維陣列的賦值邏輯一致。
3. 跨行跨列:需要跨越時,把 android:layout_rowSpan 或 android:layout_columnSpan 設為數值,再把 layout_gravity 設為 fill,前者決定跨越幾行幾列,後者讓控件填滿整個跨越範圍。
1. 建立一個空白 Activity。

2. 打開 res/layout/activity_main.xml,改成 GridLayout 結構。

3. 逐段拆解:
① XML 序言,第一行的 <?xml version="1.0" encoding="utf-8"?> 表示依 1.0 版規則解析,編碼為 UTF-8(也可寫成 GB2312)。
② 根元素由 GridLayout 擔任。
③ layout_width 與 layout_height 設為 match_parent,讓佈局填滿整個螢幕。
④ android:orientation="horizontal" 為水平排列,垂直則寫 vertical。
⑤ 這個網格是 5 列 4 行的規模。
4. 往 GridLayout 放入 16 個 Button。


5. 找不同——把目標樣式與目前樣式比對。

① 數字 0 按鈕:目標要佔兩格,目前只佔一格。
<Button
android:id="@+id/zero"
android:layout_columnSpan="2" // 橫向擴展兩列
android:layout_gravity="fill" // 填滿這兩格
android:text="0" />
② 小數點按鈕:目標在第 4 列第 3 行,目前落在第 4 列第 2 行。解法很直覺——0 按鈕佔掉兩格後,小數點會自動被往後推到位。
③ 加號按鈕:目標在第 4 列第 4 行,並縱向擴展兩行。同樣地,0 佔兩格後加號會自動補到正確位置,再加上跨行設定即可。
<Button
android:id="@+id/plus"
android:layout_rowSpan="2" // 縱向擴展兩行
android:layout_gravity="fill" // 填滿這兩格
android:text="+" />
④ 等號按鈕:目標在第 5 列並佔據 3 個欄位。位置同樣靠前面的擴展自然帶到,欄位則用 columnSpan 處理。
<Button
android:id="@+id/equal"
android:layout_columnSpan="3" // 橫向擴展三列
android:layout_gravity="fill" // 填滿這三格
android:text="=" />
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" // 水平方向
android:rowCount="5" // 5 列
android:columnCount="4"> // 4 行
<Button android:id="@+id/one" android:text="1" />
<Button android:id="@+id/two" android:text="2" />
<Button android:id="@+id/three" android:text="3" />
<Button android:id="@+id/devide" android:text="/" />
<Button android:id="@+id/four" android:text="4" />
<Button android:id="@+id/five" android:text="5" />
<Button android:id="@+id/six" android:text="6" />
<Button android:id="@+id/multiply" android:text="×" />
<Button android:id="@+id/seven" android:text="7" />
<Button android:id="@+id/eight" android:text="8" />
<Button android:id="@+id/nine" android:text="9" />
<Button android:id="@+id/minus" android:text="-" />
<Button
android:id="@+id/zero"
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:text="0" />
<Button android:id="@+id/point" android:text="." />
<Button
android:id="@+id/plus"
android:layout_rowSpan="2"
android:layout_gravity="fill"
android:text="+" />
<Button
android:id="@+id/equal"
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:text="=" />
</GridLayout>
6. 最終顯示效果:

除了 XML,GridLayout 也可以用 Java 動態組出相同畫面,作法與先前章節的 LinearLayout 程式化佈局一致:new 出容器、設定 LayoutParams、逐一 addView。此處暫略,留給你自行練習。
AbsoluteLayout 就像在 CSS 裡指定 position: absolute,靠 X、Y 座標決定元素位置。它能精準擺放,卻完全無法適配不同螢幕尺寸,因此早已不建議使用,知道有這東西即可。
WebView 錯誤碼處理:友好呈現 404、403 等異常頁面
本節引言 如果你的公司做的是 HTML5 類型的移動應用,主要依靠 WebView 來展示網頁,那麼當訪問的頁面不存在、或出現其他錯誤並返回 404、401、403、30
2.5.8 Notification 狀態列通知詳解
前言 本節要介紹的是 Android 中用於在狀態列顯示通知的元件:Notification。大部分學 Android 的人對它都不陌生,但網路上流通的教學多半基於 2
Android LinearLayout 線性布局:weight 權重、分割線與常見陷阱
LinearLayout 的核心概念LinearLayout 會將子元件排列成一條線,可設定水平或垂直。它最實用的特性是 weight 權重,常用於畫面比例分配與螢幕適配
WebView(網頁視圖)基本用法大全
本節引言 本節要帶給大家的是 Android 中用於顯示網頁的控件:WebView(網頁視圖)。 如今 Android 應用層開發主要有兩個方向:客戶端開發和 HTML5