編輯:關於Android編程
上一篇已經基本給大家介紹了如何自定義ViewGroup,如果你還不了解,請查看:Android 手把手教您自定ViewGroup ,本篇將使用上篇介紹的方法,給大家帶來一個實例:實現FlowLayout,何為FlowLayout,如果對Java的Swing比較熟悉的話一定不會陌生,就是控件根據ViewGroup的寬,自動的往右添加,如果當前行剩余空間不足,則自動添加到下一行。有點所有的控件都往左飄的感覺,第一行滿了,往第二行飄~所以也叫流式布局。Android並沒有提供流式布局,但是某些場合中,流式布局還是非常適合使用的,比如關鍵字標簽,搜索熱詞列表等,比如下圖:


這些都特別適合使用FlowLayout,本篇博客會帶領大家自己實現FlowLayout,然後使用我們自己定義的FlowLayout實現上面的標簽效果。對了,github已經有了這樣FlowLayout,但是我覺得絲毫不影響我們對其的學習,學會使用一個控件和學會寫一個控件,我相信大家都明白,授人以魚不如授人以漁。
1、對於FlowLayout,需要指定的LayoutParams,我們目前只需要能夠識別margin即可,即使用MarginLayoutParams.
2、onMeasure中計算所有childView的寬和高,然後根據childView的寬和高,計算自己的寬和高。(當然,如果不是wrap_content,直接使用父ViewGroup傳入的計算值即可)
3、onLayout中對所有的childView進行布局。
因為我們只需要支持margin,所以直接使用系統的MarginLayoutParams
@Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
{
return new MarginLayoutParams(getContext(), attrs);
}
/**
* 負責設置子控件的測量模式和大小 根據所有子控件設置自己的寬和高
*/
@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);
Log.e(TAG, sizeWidth + , + sizeHeight);
// 如果是warp_content情況下,記錄寬和高
int width = 0;
int height = 0;
/**
* 記錄每一行的寬度,width不斷取最大寬度
*/
int lineWidth = 0;
/**
* 每一行的高度,累加至height
*/
int lineHeight = 0;
int cCount = getChildCount();
// 遍歷每個子元素
for (int i = 0; i < cCount; i++)
{
View child = getChildAt(i);
// 測量每一個child的寬和高
measureChild(child, widthMeasureSpec, heightMeasureSpec);
// 得到child的lp
MarginLayoutParams lp = (MarginLayoutParams) child
.getLayoutParams();
// 當前子空間實際占據的寬度
int childWidth = child.getMeasuredWidth() + lp.leftMargin
+ lp.rightMargin;
// 當前子空間實際占據的高度
int childHeight = child.getMeasuredHeight() + lp.topMargin
+ lp.bottomMargin;
/**
* 如果加入當前child,則超出最大寬度,則的到目前最大寬度給width,類加height 然後開啟新行
*/
if (lineWidth + childWidth > sizeWidth)
{
width = Math.max(lineWidth, childWidth);// 取最大的
lineWidth = childWidth; // 重新開啟新行,開始記錄
// 疊加當前高度,
height += lineHeight;
// 開啟記錄下一行的高度
lineHeight = childHeight;
} else
// 否則累加值lineWidth,lineHeight取最大高度
{
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);
}
onLayout中完成對所有childView的位置以及大小的指定
/** * 存儲所有的View,按行記錄 */ private List> mAllViews = new ArrayList
>(); /** * 記錄每一行的最大高度 */ private List
mLineHeight = new ArrayList (); @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; // 存儲每一行所有的childView List lineViews = new ArrayList (); 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) { // 記錄這一行所有的View以及最大高度 mLineHeight.add(lineHeight); // 將當前行的childView保存,然後開啟新的ArrayList保存下一行的childView mAllViews.add(lineViews); lineWidth = 0;// 重置行寬 lineViews = new ArrayList (); } /** * 如果不需要換行,則累加 */ 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++) { // 每一行的所有的views lineViews = mAllViews.get(i); // 當前行的最大高度 lineHeight = mLineHeight.get(i); Log.e(TAG, 第 + i + 行 : + lineViews.size() + , + lineViews); Log.e(TAG, 第 + i + 行, : + lineHeight); // 遍歷當前行所有的View for (int j = 0; j < lineViews.size(); j++) { View child = lineViews.get(j); if (child.getVisibility() == View.GONE) { continue; } MarginLayoutParams lp = (MarginLayoutParams) child .getLayoutParams(); //計算childView的left,top,right,bottom int lc = left + lp.leftMargin; int tc = top + lp.topMargin; int rc =lc + child.getMeasuredWidth(); int bc = tc + child.getMeasuredHeight(); Log.e(TAG, child + , l = + lc + , t = + t + , r = + rc + , b = + bc); child.layout(lc, tc, rc, bc); left += child.getMeasuredWidth() + lp.rightMargin + lp.leftMargin; } left = 0; top += lineHeight; } }
mLineHeight記錄的為每行的最大高度。
23-48行,遍歷所有的childView,用於設置allViews的值,以及mLineHeight的值。
57行,根據allViews的長度,遍歷所有的行數
67-91行,遍歷每一行的中所有的childView,對childView的left , top , right , bottom 進行計算,和定位。
92-93行,重置left和top,准備計算下一行的childView的位置。
好了,到此完成了所有的childView的繪制區域的確定,到此,我們的FlowLayout的代碼也結束了~~靜下心來看一看是不是也不難~
我准備使用TextView作為我們的標簽,所以為其簡單寫了一點樣式:
res/values/styles.xml中:

是不是還不錯,下面繼續簡單自定義幾個背景:
res/drawble/flog_02.xml

暫不贊~~上面都是match_parent~~下面固定下寬度,實現文章最開始的移動開發熱門標簽:
flag_04.xml

是不是完全相同~~o了~
如果你覺得本篇博客對你有用,那麼就留個言或者頂一個~~
android屏幕分類與屏幕相關參數定義
android設備運行在各種不同的屏幕中,這些屏幕有著不同的screen sizes(屏幕大小)和screen densities(屏幕密度)。screen sizes表
Android HttpClient自動登陸discuz論壇!
你登陸論壇的時候,我們先看看浏覽器干了什麼事兒: 用Firefox打開HiPda 的登陸頁面,輸入用戶名和密碼,點登陸。 下面是通過firebug插件獲取的數據: 可
Android UI開發神兵利器之Android Action Bar Style Generator
ActionBar是3.0後的UI設計規范,同時也是Google極力推薦使用的設計風格,如何快速設計一個入眼的ActionBar呢,更進一步,給我們搭好一個入眼的Acti
Android開發常用功能
1.定時器的實現(1)采用Handler的postDelayed(Runnable, long)方法 Handler handler =&n