Android-通讯录:在群发短信选取联系人界面用到的自定义viewgroup

来源:互联网 发布:淘宝手机助手官网 编辑:程序博客网 时间:2024/06/01 21:53
class MyViewGroup extends ViewGroup {private final static int VIEW_MARGIN = 2;private int maxWidth = 0;private int maxHeight = 60;public MyViewGroup(Context context) {super(context);}protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {for (int index = 0; index < getChildCount(); index++) {final View child = getChildAt(index);child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);}super.onMeasure(widthMeasureSpec, heightMeasureSpec);}protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {final int count = getChildCount();int row = 0;// which row lay you view relative to parentint lengthX = arg1;    // right position of child relative to parentint lengthY = arg2;    // bottom position of child relative to parentfor(int i = 0 ; i < count ; i++){final View child = this.getChildAt(i);int width = child.getMeasuredWidth();//            int height = child.getMeasuredHeight();int height = maxHeight; //限制子节点的高度lengthX += width + VIEW_MARGIN;lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height + arg2;if(width + VIEW_MARGIN > maxWidth){maxWidth = width + VIEW_MARGIN;}if(lengthX > arg3){lengthX = width + VIEW_MARGIN + arg1;row ++;lengthY = row * (height + VIEW_MARGIN) + VIEW_MARGIN + height + arg2;}child.layout(lengthX - width, lengthY - height, lengthX, lengthY);}}}



0 0