前言
最近项目中有个需求,就是可以给用户动态添加标签。标签最大的特点就是横向排列,并且可以自动换行,而且标签的内容自定义,所以标签的长度是不固定的。
网上这种开源的一抓一大把,懒得找了,所以自己实现了一个。
先看一下效果
分析问题
首先先分析一下这个布局的特点:最大的特点就是自动换行。所以需要根据子view的宽度,计算换行的时机,并根据换行后的子View的高度计算布局的高度。
所以自定义ViewGroup,只需要重写onMeasure方法和onLayout方法即可。
注意我这里有个前提:每个子View(标签)的Margin属性分别一样
解决问题
1、onMeasure方法
在onMeasure方法中计算换行的时机,并计算总高度。设置layout的高度。
这种布局的宽度一般都是一定的大小,layout_width属性不是match_parent就是固定的值
所以宽度不用特殊处理。并且还有一个隐藏的点、一般这种需求中,子View(即标签)的高度是一致的。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
int width = MeasureSpec.getSize(widthMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
mLineCount = 0;
mChildCount = getChildCount();
//没有子控件的时候,给AT_MOST模式默认大小
if (mChildCount == 0){
if (heightMode == MeasureSpec.AT_MOST){
height = 0;
}
if (widthMode == MeasureSpec.AT_MOST){
width = 0;
}
setMeasuredDimension(width,height);
return;
}
//至少有一行
mLineCount ++;
//每一行子view的宽度加左右padding的值
int totalWidthPerLine = getPaddingLeft() + getPaddingRight();
LinearLayout.LayoutParams layoutParams = null;
View child = null;
for (int i = 0 ; i < mChildCount ; i ++){
child = getChildAt(i);
layoutParams = ((LayoutParams) child.getLayoutParams());
measureChild(child,widthMeasureSpec,heightMeasureSpec);
totalWidthPerLine += child.getMeasuredWidth()+layoutParams.leftMargin+layoutParams.rightMargin;
if (totalWidthPerLine > width){//需要换行
//换行后,当前子view需要排到新的一行,totalWidthPerLine也需要更新为新行的宽度,需要加上当前子view的宽度和margin
totalWidthPerLine = child.getMeasuredWidth()+layoutParams.leftMargin+layoutParams.rightMargin+getPaddingLeft()+getPaddingRight();
mLineCount ++;
}else{
}
}
//此viewGroup的高度
int layoutHeight = getPaddingTop() + getPaddingBottom();
//在这里每一个子view的高度一致、故只取第一个子view的高度及其marginTop和marginBottom属性
child = getChildAt(0);
layoutParams = ((LayoutParams) child.getLayoutParams());
//计算此viewGroup的高度 通过每一行子view的高度 x 行数 + padding 得到总高度
layoutHeight += (child.getMeasuredHeight() + layoutParams.topMargin + layoutParams.bottomMargin) * mLineCount;
//AT_MOST模式下、就取上面计算出来的高度
if (heightMode == MeasureSpec.AT_MOST){
height = layoutHeight;
}
this.mWidth = width;
this.mHeight = height;
setMeasuredDimension(width,height);
}
1、onLayout方法
在onLayout方法中,需要计算每一个子View位于第几行、第几列,计算在第几行,就知道当前子View的top属性,计算在第几列就知道当前子View的left属性,这样就确定了子View的位置了。
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed,l,t,r,b);
if (mLineCount > 1){
View child;
LinearLayout.LayoutParams layoutParams = null;
int[] childCountPerLine = new int[mLineCount];//每行的个数
int currentLine = 0;
int hasInArrayCount = 0;//已入数组的个数
int childrenWidth = getPaddingLeft() + getPaddingRight();
for (int i = 0 ; i < mChildCount ; i ++){
child = getChildAt(i);
layoutParams = ((LayoutParams) child.getLayoutParams());
childrenWidth += child.getMeasuredWidth() + layoutParams.leftMargin + layoutParams.rightMargin;
if (childrenWidth > mWidth){
childCountPerLine[currentLine] = i - hasInArrayCount;
hasInArrayCount += i - hasInArrayCount;
currentLine ++;
childrenWidth = getPaddingLeft() + getPaddingRight() + child.getMeasuredWidth() + layoutParams.leftMargin + layoutParams.rightMargin;
}
}
//计算最后一行有多少个子view
if (currentLine == childCountPerLine.length - 1){
childCountPerLine[currentLine] = mChildCount - hasInArrayCount;
}
int left;
int top;
int bottom;
int totalCountInLines = 0;//遍历过的每一行的子view的个数的和
//由于默认每一个子view的高度和margin属性都一致。所以选取第一个子view测量即可
View c = getChildAt(0);
LayoutParams lp = ((LayoutParams) c.getLayoutParams());
//遍历行
for (int j = 0 ; j < childCountPerLine.length ; j ++){
top = j * (c.getMeasuredHeight()+lp.topMargin+lp.bottomMargin) + lp.topMargin;
bottom = c.getMeasuredHeight() + top;
left = 0;
for (int i = totalCountInLines; i < childCountPerLine[j] + totalCountInLines ; i ++){//遍历每行的子view,布局每行子view
child = getChildAt(i);
layoutParams = ((LayoutParams) child.getLayoutParams());
left += layoutParams.leftMargin;
child.layout(left+getPaddingLeft(),top + getPaddingTop(),left+child.getMeasuredWidth(),bottom);
left += child.getMeasuredWidth() + layoutParams.rightMargin;
}
totalCountInLines += childCountPerLine[j];//将当前行子view的个数相加
}
//最后一行剩余的子view个数
int countLastLine = mChildCount - totalCountInLines;
if ( countLastLine > 0){
left = 0;
top = (childCountPerLine.length) * (c.getMeasuredHeight()+lp.topMargin+lp.bottomMargin) + lp.topMargin;
bottom = c.getMeasuredHeight() + top;
//遍历最后一行
for (int i = totalCountInLines ; i < mChildCount ; i ++){
child = getChildAt(i);
layoutParams = ((LayoutParams) child.getLayoutParams());
left += layoutParams.leftMargin;
child.layout(left+getPaddingLeft(),top + getPaddingTop(),left+child.getMeasuredWidth(),bottom);
left += child.getMeasuredWidth() + layoutParams.rightMargin;
}
}
}
}
以上就是全部内容了,比较简单。