public class MyView extends ViewGroup {
private int mleftMargin=20;
private int mtopMargin=20;
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs,0);
}
public MyView(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
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return super.generateLayoutParams(attrs);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int leftMargin=mleftMargin;
int topMargin=mtopMargin;
for (int i=0;i<getChildCount();i++){
int measureWidth=getChildAt(i).getMeasuredWidth();
int measureHeight=getChildAt(i).getMeasuredHeight();
if(leftMargin+mleftMargin+measureWidth>getWidth()){
leftMargin=mleftMargin;
topMargin+=measureHeight+mtopMargin;
getChildAt(i).layout(leftMargin,topMargin,leftMargin+measureWidth,topMargin+measureHeight);
}else {
getChildAt(i).layout(leftMargin,topMargin,leftMargin+measureWidth,topMargin+measureHeight);
}
leftMargin+=measureWidth+mleftMargin;
}
}
}