public class Zidingyi extends ViewGroup {
public Zidingyi(Context context) {
super(context);
}
public Zidingyi(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Zidingyi(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
int witth = getWidth();
int low = 0;//定义常量
int diswith = 18;//定义行间距
for (int j = 0; j < getChildCount(); j++) {
View child = getChildAt(j);
int width = child.getMeasuredWidth();
int height = child.getMeasuredHeight();
if (diswith + width > witth) {
low++;
diswith = 18;
}
child.layout(diswith, low * height, diswith + width, height * (low + 1));
diswith += width;
}
}
}