自定义换行布局,直接上代码
package com.sbnnest.view;
import java.util.Hashtable;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
/**
*
* 自动换行布局
*
* @author Alex.Lu
* @version V1.0
*/
public class NewlineLayout extends ViewGroup {
Hashtable<View, ViewPostion> mMap = new Hashtable<View, NewlineLayout.ViewPostion>();
public NewlineLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public NewlineLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NewlineLayout(Context context) {
super(context);
}
@Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
{
return new MarginLayoutParams(getContext(), attrs);
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
// TODO Auto-generated method stub
return new MarginLayoutParams(MarginLayoutParams.WRAP_CONTENT, MarginLayoutParams.WRAP_CONTENT);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
ViewPostion pos = mMap.get(child);
if (pos != null) {
child.layout(pos.getLeft(), pos.getTop(), pos.getRight(), pos.getBottom());
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int maxH = 0;
int left, right, top, bottom;
int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight();
left = getPaddingLeft();
top = getPaddingTop();
right = 0;
bottom = 0;
int count = getChildCount();
mMap.clear();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
if(child.getVisibility() == View.GONE){
continue;
}
MarginLayoutParams mp = (MarginLayoutParams) child.getLayoutParams();
measureChildWithMargins(child, widthMeasureSpec, 0,heightMeasureSpec, 0);
left = left + mp.leftMargin;
right = left + child.getMeasuredWidth() + mp.rightMargin;
maxH = Math.max(maxH, child.getMeasuredHeight());
/**
* 如果大于宽度就
*/
if (right > width) {
left = getPaddingLeft() + mp.leftMargin;
right = left + child.getMeasuredWidth();
top += maxH;
maxH = 0;
}
bottom = top + child.getMeasuredHeight();
ViewPostion position = new ViewPostion();
position.setPostion(left, top, right, bottom);
left = right;
mMap.put(child, position);
}
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), bottom + getPaddingBottom());
}
/**
*
* 存储布局位置信息
*
* @author Alex.Lu
*
*/
private class ViewPostion {
private int mLeft, mTop, mRight, mBottom;
public void setPostion(int left,int top,int right,int bottom){
mLeft = left;
mTop = top;
mRight = right;
mBottom = bottom;
}
public int getLeft() {
return mLeft;
}
public int getTop() {
return mTop;
}
public int getRight() {
return mRight;
}
public int getBottom() {
return mBottom;
}
}
}
1.先调用onMeasure方法,在测量每个布局的大小,并把位置给记录下来放在map里。
2.调用onLayout方法,把map中的每一个子组件进行布局。
注意需要重写generateLayoutParams的两个方法,否则会报错误。