public class SkillGridLayout extends ViewGroup {
int mLeft, mRight, mTop, mBottom;
private int mPaddingLeft = ScreenUtils.dip2px(4.0f);
private int mPaddingTop = 5;
Hashtable map = new Hashtable();
public SkillGridLayout(Context context) {
super(context);
}
public SkillGridLayout(Context context, int horizontalSpacing, int verticalSpacing) {
super(context);
}
public SkillGridLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int mWidth = MeasureSpec.getSize(widthMeasureSpec);
int mCount = getChildCount();
int mX = 0;
int mY = 0;
mLeft = 0;
mRight = 0;
mTop = 5;
mBottom = 0;
int j = 0;
View lastview = null;
if (mCount > 0) {
View child_one = getChildAt(0);
child_one.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
// 此处增加onlayout中的换行判断,用于计算所需的高度
mX += child_one.getMeasuredWidth();
mY += child_one.getMeasuredHeight();
Position position_one = new Position();
mLeft = 0;
mRight = mLeft + child_one.getMeasuredWidth();
mBottom = mTop + child_one.getMeasuredHeight();
mY = mTop; // 每次的高度必须记录 否则控件会叠加到一起
position_one.left = mLeft;
position_one.top = mTop + 3;
position_one.right = mRight;
position_one.bottom = mBottom;
map.put(child_one, position_one);
}
for (int i = 1; i < mCount; i++) {
final View child = getChildAt(i);
child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
// 此处增加onlayout中的换行判断,用于计算所需的高度
int childw = child.getMeasuredWidth() + mPaddingLeft;
int childh = child.getMeasuredHeight();
mX += childw; // 将每次子控件宽度进行统计叠加,如果大于设定的高度则需要换行,高度即Top坐标也需重新设置
Position position = new Position();
mLeft = getPosition(i - j, i);
mRight = mLeft + child.getMeasuredWidth();
if (mX > mWidth) {
mX = child.getMeasuredWidth();
mY += childh;
j = i;
mLeft = 0;
mRight = mLeft + child.getMeasuredWidth();
mTop = mY + mPaddingTop;
// PS:如果发现高度还是有问题就得自己再细调了
}
mBottom = mTop + child.getMeasuredHeight();
mY = mTop; // 每次的高度必须记录 否则控件会叠加到一起
position.left = mLeft;
position.top = mTop + 3;
position.right = mRight;
position.bottom = mBottom;
map.put(child, position);
}
setMeasuredDimension(mWidth, mBottom);
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(1, 1); // default of 1px spacing
}
@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);
Position pos = (Position) map.get(child);
if (pos != null) {
child.layout(pos.left, pos.top, pos.right, pos.bottom);
} else {
Log.i("MyLayout", "error");
}
}
}
private class Position {
int left, top, right, bottom;
}
public int getPosition(int IndexInRow, int childIndex) {
if (IndexInRow > 0) {
return getPosition(IndexInRow - 1, childIndex - 1) + getChildAt(childIndex - 1).getMeasuredWidth() + mPaddingLeft;
}
return getPaddingLeft();
}
/**
* 设置之控件的左右边距
* @param paddingLeft
*/
public void setPaddingLeft(int paddingLeft) {
mPaddingLeft = ScreenUtils.dip2px(paddingLeft);
}
/**
* 设置之控件的上下边距
* @param paddingTop
*/
public void setPaddingTop(int paddingTop) {
mPaddingTop = ScreenUtils.dip2px(mPaddingTop);
}
}
需要设置左右边距的大小实现setPaddingLeft()方法,上下间距实现setPaddingTop()方法
子控件自动换行的父容器
最新推荐文章于 2020-01-10 10:44:35 发布
3291

被折叠的 条评论
为什么被折叠?



