編輯:關於 Android 程式設計
FlowLayout(流式布局)指的是控件會依據 ViewGroup 的寬度自動向右排列,當目前行剩餘空間不足時便自動換到下一行,有點像所有控件都往左飄、第一行滿了就飄到第二行的感覺。對於熟悉 Java Swing 的開發者,這個概念並不陌生。
Android 並未內建流式布局,但在某些場合(如關鍵字標籤、搜尋熱詞列表)它卻格外合用。本篇就帶大家一起動手實現 FlowLayout,並用它打造標籤效果。網路上雖已有現成的 FlowLayout,但「學會用一個控件」與「學會寫一個控件」終究不同——授人以魚不如授人以漁。


1、FlowLayout 只需識別 margin,因此使用 MarginLayoutParams 即可;
2、在 onMeasure 中計算所有子 View 的寬高,進而算出自己的寬高(若非 wrap_content,直接用父容器傳入的值);
3、在 onLayout 中對所有子 View 進行布局。
由於只需支持 margin,直接使用系統的 MarginLayoutParams:
@Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(), attrs);
}
onMeasure 負責設置子控件的測量模式與大小,並據此計算自己的寬高:
@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 width = 0;
int height = 0;
int lineWidth = 0; // 記錄每一行的寬度,width 不斷取最大寬度
int lineHeight = 0; // 每一行的高度,累加至 height
int cCount = getChildCount();
for (int i = 0; i < cCount; i++) {
View child = getChildAt(i);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
// 若加入當前 child 會超出最大寬度,則記錄目前最大寬度並開啟新行
if (lineWidth + childWidth > sizeWidth) {
width = Math.max(lineWidth, childWidth);
lineWidth = childWidth;
height += lineHeight;
lineHeight = childHeight;
} else {
lineWidth += childWidth;
lineHeight = Math.max(lineHeight, childHeight);
}
// 若是最後一個,則比較當前記錄的最大寬度與當前 lineWidth
if (i == cCount - 1) {
width = Math.max(width, lineWidth);
height += lineHeight;
}
}
setMeasuredDimension(
(modeWidth == MeasureSpec.EXACTLY) ? sizeWidth : width,
(modeHeight == MeasureSpec.EXACTLY) ? sizeHeight : height);
}
首先取得父容器傳入的測量模式與計算值,遍歷所有子 View 並以 measureChild 測量,據此得出 wrap_content 時的寬高。最後依模式決定:若為 EXACTLY 則直接用父容器傳入值,否則用自己計算的寬高。
onLayout 完成對所有子 View 的位置與大小指定。這裡用 mAllViews 按行記錄所有子 View,用 mLineHeight 記錄每行的最大高度:
private List<List<View>> mAllViews = new ArrayList<List<View>>();
private List<Integer> mLineHeight = new ArrayList<Integer>();
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
mAllViews.clear();
mLineHeight.clear();
int width = getWidth();
int lineWidth = 0;
int lineHeight = 0;
List<View> lineViews = new ArrayList<View>();
int cCount = getChildCount();
for (int i = 0; i < cCount; i++) {
View child = getChildAt(i);
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
// 若已經需要換行
if (childWidth + lp.leftMargin + lp.rightMargin + lineWidth > width) {
mLineHeight.add(lineHeight);
mAllViews.add(lineViews);
lineWidth = 0; // 重置行寬
lineViews = new ArrayList<View>(); // 開啟新行
}
lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
lineHeight = Math.max(lineHeight, childHeight + lp.topMargin + lp.bottomMargin);
lineViews.add(child);
}
// 記錄最後一行
mLineHeight.add(lineHeight);
mAllViews.add(lineViews);
int left = 0;
int top = 0;
int lineNums = mAllViews.size();
for (int i = 0; i < lineNums; i++) {
lineViews = mAllViews.get(i);
lineHeight = mLineHeight.get(i);
for (int j = 0; j < lineViews.size(); j++) {
View child = lineViews.get(j);
if (child.getVisibility() == View.GONE) continue;
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int lc = left + lp.leftMargin;
int tc = top + lp.topMargin;
int rc = lc + child.getMeasuredWidth();
int bc = tc + child.getMeasuredHeight();
child.layout(lc, tc, rc, bc);
left += child.getMeasuredWidth() + lp.rightMargin + lp.leftMargin;
}
left = 0;
top += lineHeight;
}
}
其中 mAllViews 的每個 Item 為每行所有 View 的 List 集合,mLineHeight 記錄每行最大高度。遍歷所有子 View 完成分行後,再依行逐一遍歷子 View,計算並定位其 left、top、right、bottom,最後重置 left 與 top 以計算下一行。
我們用 TextView 作為標籤,先為其定義樣式與圓角背景:
<style name="text_flag_01">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_margin">4dp</item>
<item name="android:background">@drawable/flag_01</item>
<item name="android:textColor">#ffffff</item>
</style>
並在布局中以自定義的 FlowLayout 承載多個 TextView 標籤,即可呈現自動換行的流式效果:

接著替換不同背景(flag_02、flag_03、flag_04),搭配不同文字顏色,就能做出多種風格的標籤:

若把 FlowLayout 的寬度固定為 200dp,還能實現文章開頭那種「移動開發熱門標籤」的效果:

至此,我們的 FlowLayout 便完成了。回過頭看,只要掌握 onMeasure 的測量分行與 onLayout 的定位計算,自定義流式布局其實並不難。理解了這套原理,你也能輕鬆擴展出更豐富的自定義 ViewGroup。
Android 實現炫酷的直播彈幕效果(DanmakuFlameMaster 實戰)
網絡直播越來越火,主播也逐漸成了一種新興職業。而做直播,彈幕幾乎是標配功能,效果大概如下圖: 一、先想清楚彈幕是怎麼實現的 動手之前先把結構拆開看。最底層肯定是視頻播放
Android 生成 keystore 的兩種方式:Eclipse 圖形化與命令行
發布前必須準備好簽名文件。這裡整理兩條路徑:一條是在 Eclipse 裡用向導點完就好,另一條是用 keytool 命令直接生成。 一、在 Eclipse 中生成 先打開
Android記憶體快取算法解析:從LRU、LFU到ImageLoader實作
前言 在Android圖片加載與資料快取場景中,記憶體快取扮演關鍵角色。適當的快取能減少重複加載,提高使用者體驗;但若策略不當,也可能造成記憶體壓力。本文以常見快取演算法
Android Drawable 全解析:六種常見派生類的用法
一、Drawable 簡介 Android 中的 Drawable 種類繁多。它可以是一張位圖,也可以是由顏色和形狀構造出來的圖形,因此有時候它並沒有明確的「尺寸」概念。