編輯:關於 Android 程式設計
本篇延續自定義 ViewGroup 的系列主題,動手做一個實用的範例:FlowLayout(流式版面配置)。
所謂 FlowLayout,指的是子控制項會依序往右排入容器,一旦目前這一行剩下的寬度不夠放下一個子 View,就自動換到下一行繼續排列。因為像水流一樣逐行填滿,所以又被稱為流式版面配置。
Android SDK 本身並沒有內建流式版面配置,但在不少場景裡它卻格外好用,例如關鍵字標籤、搜尋熱詞列表、文章分類標籤等等。如下圖所示的標籤群組:

LayoutParams 的定義與 onLayout 的寫法,和上一篇 WaterfallLayout 幾乎完全一致,這裡不再重複,有興趣的讀者可以回頭參考《Android 自定義 ViewGroup(二)之 WaterfallLayout》。
真正需要花心思的是 onMeasure:我們要在測量階段一邊累加每個子 View 的寬高,一邊判斷是否需要換行,同時把每個子 View 的座標先算好存進 LayoutParams,留給 onLayout 使用。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 取得父容器為我們設定的測量模式與尺寸
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
int childCount = getChildCount();
// wrap_content 情況下用來記錄整體寬高
int wrapWidth = 0;
int wrapHeight = 0;
// 目前這一行累積的寬度(含右側 hSpace)
int lineWidth = 0;
// 目前這一行的高度,取本行最高的子 View
int lineHeight = 0;
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
LayoutParams lParams = (LayoutParams) child.getLayoutParams();
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
if (lineWidth + childWidth > sizeWidth) {
// 放不下了,換行:記錄新行第一個標籤的座標
lParams.left = 0;
lParams.top = wrapHeight + lineHeight + vSpace;
lParams.right = childWidth;
lParams.bottom = lParams.top + childHeight;
// 注意 lineWidth 含右側 hSpace,比較時要減掉
wrapWidth = Math.max(lineWidth - hSpace, childWidth);
// 重新開始記錄新行
lineWidth = childWidth + hSpace;
wrapHeight += lineHeight + vSpace;
lineHeight = childHeight;
} else {
// 仍在同一行,往右追加
lParams.left = lineWidth;
lParams.top = wrapHeight;
lParams.right = lParams.left + childWidth;
lParams.bottom = lParams.top + childHeight;
lineWidth += childWidth + hSpace;
lineHeight = Math.max(lineHeight, childHeight);
}
// 最後一個子 View 需要收尾
if (i == childCount - 1) {
wrapWidth = Math.max(wrapWidth, lineWidth - hSpace);
wrapHeight += lineHeight;
}
}
setMeasuredDimension(
(modeWidth == MeasureSpec.EXACTLY) ? sizeWidth : wrapWidth,
(modeHeight == MeasureSpec.EXACTLY) ? sizeHeight : wrapHeight);
}
用法非常直觀,直接在 XML 裡把子 View 寫進去即可:
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:attr="https://schemas.android.com/apk/res/com.hx.flowlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E1E6F6"
android:orientation="vertical">
<com.hx.flowlayout.FlowLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
attr:hSpace="20"
attr:vSpace="10">
<TextView android:text="標簽" />
<TextView android:text="Welcome" />
<TextView android:text="IT工程師" />
<TextView android:text="程序猿" />
<TextView android:text="Android" />
<TextView android:text="Java" />
<TextView android:text="ViewGroup" />
<TextView android:text="FlowLayout" />
</com.hx.flowlayout.FlowLayout>
</LinearLayout>
上面這種寫法雖然直白,但每個標籤都寫死在 XML 裡顯得冗長。實務上更常見的做法是透過 Java 程式碼動態 addView,資料有多少就產生多少個標籤。
接著定義兩種標籤風格,透過 style 讓外觀與版面分離:
<style name="flow_text_style_1">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@drawable/flow_text_bg_1</item>
<item name="android:textColor">#ffffff</item>
<item name="android:textSize">16sp</item>
</style>
<style name="flow_text_style_2">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@drawable/flow_text_bg_2</item>
<item name="android:textColor">#4B0082</item>
<item name="android:textSize">20sp</item>
</style>
背景使用 shapeDrawable 畫出圓角膠囊狀的標籤底色,圓角、描邊、內距都能自由調整:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<corners android:radius="40dp"/>
<stroke android:color="#C9C9C9" android:width="2dp"/>
<padding android:bottom="2dp" android:left="10dp" android:right="10dp" android:top="2dp" />
</shape>
最終執行結果如下:

原始碼下載:https://xiazai.jb51.net/201609/yuanma/Android-FlowLayout(jb51.net).rar
掌握 onMeasure 中的換行判斷與座標記錄之後,FlowLayout 這類自定義 ViewGroup 就不再神祕;只要再補上 margin 支援與 padding 計算,就能延伸出更完整的標籤元件。
Android 文件下載入門:GET 請求的三個關鍵要點
從服務器拉取一個文本或文件到本地,是 Android 網絡編程最基礎的需求之一。實踐中真正容易出錯的地方並不多,但每一個都足以讓程序崩潰。 三個必須記住的要點 1. 不
XCL-Charts:以原生 Canvas 打造 Android 開源圖表庫
專案概要 XCL-Charts 是一套以 Android 原生 Canvas 繪製的開源圖表庫,支援柱形圖、餅圖、堆疊圖、面積圖、折線圖、曲線圖、環形圖、玫瑰圖與儀表板等
FrameLayout 搭配 Fragment:Android 主從式 UI 布局實作
把 Fragment 與 Layout 結合使用,通常是讓主 Activity 以 frame 填補的方式來管理 Fragment,形成左列表、右詳情的主從式版面。 1.
正确保存 Android View 状态:从原理到自定义实现
今天聊一个容易被忽视、但直接影响用户体验的话题:View 状态的保存与恢复。之所以单独讨论 View,是因为这个过程比保存 Activity 或 Fragment 状态要