Android 自定義ViewGroup 實戰篇 -> 實現FlowLayout
1、概述
上一篇已經基本給大家介紹瞭如何自定義ViewGroup,如果你還不瞭解,請檢視:,本篇將使用上篇介紹的方法,給大家帶來一個例項:實現FlowLayout,何為FlowLayout,如果對Java的Swing比較熟悉的話一定不會陌生,就是控制元件根據ViewGroup的寬,自動的往右新增,如果當前行剩餘空間不足,則自動新增到下一行。有點所有的控制元件都往左飄的感覺,第一行滿了,往第二行飄~所以也叫流式佈局。Android並沒有提供流式佈局,但是某些場合中,流式佈局還是非常適合使用的,比如關鍵字標籤,搜尋熱詞列表等,比如下圖:
這些都特別適合使用FlowLayout,本篇部落格會帶領大家自己實現FlowLayout,然後使用我們自己定義的FlowLayout實現上面的標籤效果。對了,github已經有了這樣FlowLayout,但是我覺得絲毫不影響我們對其的學習,學會使用一個控制元件和學會寫一個控制元件,我相信大家都明白,授人以魚不如授人以漁。
2、簡單的分析
1、對於FlowLayout,需要指定的LayoutParams,我們目前只需要能夠識別margin即可,即使用MarginLayoutParams.
2、onMeasure中計算所有childView的寬和高,然後根據childView的寬和高,計算自己的寬和高。(當然,如果不是wrap_content,直接使用父ViewGroup傳入的計算值即可)
3、onLayout中對所有的childView進行佈局。
3、generateLayoutParams
因為我們只需要支援margin,所以直接使用系統的MarginLayoutParams
@Override public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) { return new MarginLayoutParams(getContext(), attrs); }
4、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); 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); }
首先得到其父容器傳入的測量模式和寬高的計算值,然後遍歷所有的childView,使用measureChild方法對所有的childView進行測量。然後根據所有childView的測量得出的寬和高得到該ViewGroup如果設定為wrap_content時的寬和高。最後根據模式,如果是MeasureSpec.EXACTLY則直接使用父ViewGroup傳入的寬和高,否則設定為自己計算的寬和高。
5、onLayout
onLayout中完成對所有childView的位置以及大小的指定
/**
* 儲存所有的View,按行記錄
*/
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;
// 儲存每一行所有的childView
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)
{
// 記錄這一行所有的View以及最大高度
mLineHeight.add(lineHeight);
// 將當前行的childView儲存,然後開啟新的ArrayList儲存下一行的childView
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++)
{
// 每一行的所有的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;
}
}
allViews的每個Item為每行所有View的List集合。
mLineHeight記錄的為每行的最大高度。
23-48行,遍歷所有的childView,用於設定allViews的值,以及mLineHeight的值。
57行,根據allViews的長度,遍歷所有的行數
67-91行,遍歷每一行的中所有的childView,對childView的left , top , right , bottom 進行計算,和定位。
92-93行,重置left和top,準備計算下一行的childView的位置。
好了,到此完成了所有的childView的繪製區域的確定,到此,我們的FlowLayout的程式碼也結束了~~靜下心來看一看是不是也不難~
6、測試
我準備使用TextView作為我們的標籤,所以為其簡單寫了一點樣式:
res/values/styles.xml中:
<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>
flag_01.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#7690A5" >
</solid>
<corners android:radius="5dp"/>
<padding
android:bottom="2dp"
android:left="10dp"
android:right="10dp"
android:top="2dp" />
</shape>
佈局檔案:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E1E6F6"
android:orientation="vertical" >
<com.zhy.zhy_flowlayout02.FlowLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
style="@style/text_flag_01"
android:text="Welcome" />
<TextView
style="@style/text_flag_01"
android:text="IT工程師" />
<TextView
style="@style/text_flag_01"
android:text="學習ing" />
<TextView
style="@style/text_flag_01"
android:text="戀愛ing" />
<TextView
style="@style/text_flag_01"
android:text="掙錢ing" />
<TextView
style="@style/text_flag_01"
android:text="努力ing" />
<TextView
style="@style/text_flag_01"
android:text="I thick i can" />
</com.zhy.zhy_flowlayout02.FlowLayout>
</LinearLayout>
效果圖:
是不是還不錯,下面繼續簡單自定義幾個背景:
res/drawble/flog_02.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#FFFFFF" >
</solid>
<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>
flag_03.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#FFFFFF" >
</solid>
<corners android:radius="40dp"/>
<padding
android:bottom="2dp"
android:left="10dp"
android:right="10dp"
android:top="2dp" />
</shape>
佈局檔案:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E1E6F6"
android:orientation="vertical" >
<com.zhy.zhy_flowlayout02.FlowLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
style="@style/text_flag_01"
android:text="Welcome" />
<TextView
style="@style/text_flag_01"
android:text="IT工程師" />
<TextView
style="@style/text_flag_01"
android:text="學習ing" />
<TextView
style="@style/text_flag_01"
android:text="戀愛ing" />
<TextView
style="@style/text_flag_01"
android:text="掙錢ing" />
<TextView
style="@style/text_flag_01"
android:text="努力ing" />
<TextView
style="@style/text_flag_01"
android:text="I thick i can" />
</com.zhy.zhy_flowlayout02.FlowLayout>
<com.zhy.zhy_flowlayout02.FlowLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="Welcome"
android:textColor="#888888" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="IT工程師"
android:textColor="#888888" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="學習ing"
android:textColor="#888888" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="戀愛ing"
android:textColor="#888888" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="掙錢ing"
android:textColor="#888888" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="努力ing"
android:textColor="#888888" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_02"
android:text="I thick i can"
android:textColor="#888888" />
</com.zhy.zhy_flowlayout02.FlowLayout>
<com.zhy.zhy_flowlayout02.FlowLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="Welcome"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="IT工程師"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="學習ing"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="戀愛ing"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="掙錢ing"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="努力ing"
android:textColor="#43BBE7" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_03"
android:text="I thick i can"
android:textColor="#43BBE7" />
</com.zhy.zhy_flowlayout02.FlowLayout>
</LinearLayout>
效果圖:
暫不讚~~上面都是match_parent~~下面固定下寬度,實現文章最開始的移動開發熱門標籤:
flag_04.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#E7E7E7" >
</solid>
<corners
android:radius="30dp"
/>
<padding
android:bottom="2dp"
android:left="10dp"
android:right="10dp"
android:top="2dp" />
</shape>
佈局檔案:
<com.zhy.zhy_flowlayout02.FlowLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_04"
android:text="Welcome"
android:textColor="#323232" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_04"
android:text="IT工程師"
android:textColor="#323232" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_04"
android:text="學習ing"
android:textColor="#323232" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_04"
android:text="戀愛ing"
android:textColor="#323232" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_04"
android:text="掙錢ing"
android:textColor="#323232" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_04"
android:text="努力ing"
android:textColor="#323232" />
<TextView
style="@style/text_flag_01"
android:background="@drawable/flag_04"
android:text="I thick i can"
android:textColor="#323232" />
</com.zhy.zhy_flowlayout02.FlowLayout>
效果圖:
是不是完全相同~~o了~
如果你覺得本篇部落格對你有用,那麼就留個言或者頂一個~~
該部落格視訊教程已經上線,如果你還不清楚,或者動態新增控制元件有問題,可以檢視下;(PS:視訊中也fix了padding的問題):
----------------------------------------------------------------------------------------------------------
博主部分視訊已經上線,如果你不喜歡枯燥的文字,請猛戳(初錄,期待您的支援):