Android自定義ViewGroup之FlowLayout(三)
本篇繼續(xù)來講自定義ViewGroup,給大家?guī)硪粋€實例:FlowLayout。何為FlowLayout,就是控件根據(jù)ViewGroup的寬,自動的往右添加,如果當(dāng)前行剩余空間不足,則自動添加到下一行,所以也叫流式布局。Android并沒有提供流式布局,但是某些場合中,流式布局還是非常適合使用的,比如關(guān)鍵字標(biāo)簽,搜索熱詞列表等,比如下圖:

定義FlowLayout
LayoutParams,onLayout的寫法都和上一篇講WaterfallLayout一模一樣,在此不再贅述了,沒看過的可以參照上一篇Android自定義ViewGroup(二)之WaterfallLayout。
在這里主要說的是onMeasure方法,注釋見下方:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 獲得它的父容器為它設(shè)置的測量模式和大小
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;
//記錄每一行的寬度,width不斷取最大寬度
int lineWidth = 0;
//每一行的高度,累加至height
int lineHeight = 0;
// 遍歷每個子元素
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
// 測量每一個child的寬和高
measureChild(child, widthMeasureSpec, heightMeasureSpec);
// 得到child的lParams
LayoutParams lParams = (LayoutParams) child.getLayoutParams();
// 當(dāng)前子空間實際占據(jù)的寬度
int childWidth = child.getMeasuredWidth();
// 當(dāng)前子空間實際占據(jù)的高度
int childHeight = child.getMeasuredHeight();
// 如果加上當(dāng)前child,則超出最大寬度,然后開啟新行
if (lineWidth + childWidth > sizeWidth) {
//記錄新行頭一個標(biāo)簽坐標(biāo),為onLayout做準(zhǔn)備
lParams.left = 0;
lParams.top = wrapHeight + lineHeight + vSpace;
lParams.right = childWidth;
lParams.bottom = lParams.top + childHeight;
//取最大的,注意這里lineWidth是包括右側(cè)hSpace的,需要減掉
wrapWidth = Math.max(lineWidth - hSpace, childWidth);
// 重新開啟新行,開始記錄,可以看到行寬包括最右側(cè)hSpace
lineWidth = childWidth + hSpace;
// 疊加當(dāng)前高度,同理,加上下側(cè)vSpace
wrapHeight += lineHeight + vSpace;
// 開啟記錄下一行的高度
lineHeight = childHeight;
} else {
//記錄每一個標(biāo)簽坐標(biāo),為onLayout做準(zhǔn)備
lParams.left = lineWidth;
lParams.top = wrapHeight;
lParams.right = lParams.left + childWidth;
lParams.bottom = lParams.top + childHeight;
//在本行追加標(biāo)簽,累加值到lineWidth,lineHeight取最大高度
lineWidth += childWidth + hSpace;
lineHeight = Math.max(lineHeight, childHeight);
}
// 如果是最后一個
if (i == childCount - 1) {
//將當(dāng)前記錄的最大寬度和當(dāng)前l(fā)ineWidth做比較,取較大值
wrapWidth = Math.max(wrapWidth, lineWidth - hSpace);
//布局高加上最后一行高
wrapHeight += lineHeight;
}
}
setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth : wrapWidth, (modeHeight == MeasureSpec.EXACTLY) ? sizeHeight : wrapHeight);
}
使用FlowLayout
直接看xml吧,一看便知:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:attr="http://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 style="@style/flow_text_style_1" android:text="標(biāo)簽" /> <TextView style="@style/flow_text_style_1" android:text="Welcome" /> <TextView style="@style/flow_text_style_1" android:text="IT工程師" /> <TextView style="@style/flow_text_style_1" android:text="程序猿" /> <TextView style="@style/flow_text_style_1" android:text="Android" /> <TextView style="@style/flow_text_style_1" android:text="Java" /> <TextView style="@style/flow_text_style_1" android:text="ViewGroup" /> <TextView style="@style/flow_text_style_1" android:text="FlowLayout" /> </com.hx.flowlayout.FlowLayout> <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 style="@style/flow_text_style_2" android:text="標(biāo)簽" /> <TextView style="@style/flow_text_style_2" android:text="Welcome" /> <TextView style="@style/flow_text_style_2" android:text="IT工程師" /> <TextView style="@style/flow_text_style_2" android:text="程序猿" /> <TextView style="@style/flow_text_style_2" android:text="Android" /> <TextView style="@style/flow_text_style_2" android:text="Java" /> <TextView style="@style/flow_text_style_2" android:text="ViewGroup" /> <TextView style="@style/flow_text_style_2" android:text="FlowLayout" /> </com.hx.flowlayout.FlowLayout> </LinearLayout>
這里寫的比較啰嗦,所有TextView都是寫在xml里面的,當(dāng)然我們也可以通過Java代碼來動態(tài)添加。
再來看看style吧,這里我們定義了兩種不同的風(fēng)格,具體見下面:
<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>
找到background我們再進(jìn)去看看,這里使用的是shapeDrawable,之后我會寫一些關(guān)于shapeDrawable的文章:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://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>
效果圖如下:

源碼下載:http://xiazai.jb51.net/201609/yuanma/Android-FlowLayout(jb51.net).rar
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程之ICS式下拉菜單PopupWindow實現(xiàn)方法詳解(附源碼下載)
這篇文章主要介紹了Android編程之ICS式下拉菜單PopupWindow實現(xiàn)方法,結(jié)合實例詳細(xì)分析了ICS式下拉菜單的實現(xiàn)原理與相關(guān)技巧,并附帶源碼供讀者下載,需要的朋友可以參考下2015-12-12
Android中Java instanceof關(guān)鍵字全面解析
instanceof關(guān)鍵字用于判斷一個引用類型變量所指向的對象是否是一個類(或接口、抽象類、父類)的實例.這篇文章主要介紹了Android中Java instanceof關(guān)鍵字全面解析的相關(guān)資料,需要的朋友可以參考下2016-07-07
Android如何實現(xiàn)非本地圖片的點擊態(tài)
Android如何實現(xiàn)非本地圖片的點擊態(tài),本文提供了詳細(xì)的實現(xiàn)代碼,需要了解的朋友可以參考下2012-12-12

