首先聲明一下,此博文實際上是一個筆記形式的文章,思路和代碼都是由慕課網提供。敝人隻是更加詳細化地加以說明。首先上一張圖
如圖所示的布局就是流式布局。即它會自動識别每一行是否能完全填充下一個View,如果能容下,就放在上一個View的後面,如果容不下,就另起一行。這種布局讓人感覺到很清新随和。在android中可以把諸如标簽,選項,之類的空間做成這個樣子,肯定會為你的APP增色不少。下面我們來看一下這是如何實作的。
正如标題所說,需要自定義ViewGroup來實作此效果。
自定義ViewGroup的兩個步驟:
1.重載onMeasure()方法:測量子控件和自己的大小。
2.重載onLayout()方法:确定子View的位置。
下面先重載onMeasure()方法,測量子控件和自己的大小。
首先定義兩個變量:
/**
* 存儲所有的View
*/
private List<List<View>> mAllViews = new ArrayList<List<View>>();
/**
* 每一行的高度
*/
private List<Integer> mLineHeight = new ArrayList<Integer>();
重載onLayout()方法:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
// wrap_content
int width = ;
int height = ;
// 記錄每一行的寬度與高度
int lineWidth = ;
int lineHeight = ;
// 得到内部元素的個數
int cCount = getChildCount();
for (int i = ; i < cCount; i++) {
View child = getChildAt(i);
// 測量子View的寬和高
measureChild(child, widthMeasureSpec, heightMeasureSpec);
// 得到LayoutParams
MarginLayoutParams lp = (MarginLayoutParams) child
.getLayoutParams();
// 子View占據的寬度
int childWidth = child.getMeasuredWidth() + lp.leftMargin
+ lp.rightMargin;
// 子View占據的高度
int childHeight = child.getMeasuredHeight() + lp.topMargin
+ lp.bottomMargin;
//幾率每一行的View
List<View> lineViews = new ArrayList<View>();
// 換行
if (lineWidth + childWidth > sizeWidth - getPaddingLeft()
- getPaddingRight()) {
// 對比得到最大的寬度
width = Math.max(width, lineWidth);
// 重置lineWidth
lineWidth = childWidth;
// 記錄行高
height += lineHeight;
lineHeight = childHeight;
mLineHeight.add(childHeight);
} else
// 未換行
{
// 疊加行寬
lineWidth += childWidth;
// 得到目前行最大的高度
lineHeight = Math.max(lineHeight, childHeight);
}
// 最後一個控件
if (i == cCount - ) {
width = Math.max(lineWidth, width);
height += lineHeight;
}
}
setMeasuredDimension(
//
modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width
+ getPaddingLeft() + getPaddingRight(),
modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height
+ getPaddingTop() + getPaddingBottom()//
);
}
再來重載onLayout()方法,确定子View的位置。
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
mAllViews.clear();
mLineHeight.clear();
// 目前ViewGroup的寬度
int width = getWidth();
int lineWidth = ;
int lineHeight = ;
List<View> lineViews = new ArrayList<View>();
int cCount = getChildCount();
for (int i = ; i < cCount; i++) {
View child = getChildAt(i);
MarginLayoutParams lp = (MarginLayoutParams) child
.getLayoutParams();
int childWidth = child.getMeasuredWidth();
int childHeight = child.getMeasuredHeight();
// 如果需要換行
if (childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width
- getPaddingLeft() - getPaddingRight()) {
// 記錄LineHeight
mLineHeight.add(lineHeight);
// 記錄目前行的Views
mAllViews.add(lineViews);
// 重置我們的行寬和行高
lineWidth = ;
lineHeight = childHeight + lp.topMargin + lp.bottomMargin;
// 重置我們的View集合
lineViews = new ArrayList<View>();
}
lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
lineHeight = Math.max(lineHeight, childHeight + lp.topMargin
+ lp.bottomMargin);
lineViews.add(child);
}// for end
// 處理最後一行
mLineHeight.add(lineHeight);
mAllViews.add(lineViews);
// 設定子View的位置
int left = getPaddingLeft();
int top = getPaddingTop();
// 行數
int lineNum = mAllViews.size();
for (int i = ; i < lineNum; i++) {
// 目前行的所有的View
lineViews = mAllViews.get(i);
lineHeight = mLineHeight.get(i);
for (int j = ; j < lineViews.size(); j++) {
View child = lineViews.get(j);
// 判斷child的狀态
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();
// 為子View進行布局
child.layout(lc, tc, rc, bc);
left += child.getMeasuredWidth() + lp.leftMargin
+ lp.rightMargin;
}
left = getPaddingLeft();
top += lineHeight;
}
還有一個方法需要重載一下;
/**
* 與目前ViewGroup對應的LayoutParams
*/
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(), attrs);
}
(這裡有一點需要說一下,調用一個View的getLayoutParams()得到的是其父控件的LayoutParams類型,調用一個View的setLayoutParams()方法時也是得設定成父控件的LayoutParams類型。比如說在一個AbsListView中有一個Button,那麼Button的LayoutParams都是android.view.abslistview.layoutparams,而不是其他的。)
最後,Demo位址:http://download.csdn.net/detail/nsgsbs/8525125