继承ViewGroup

来源:互联网 发布:许晴 王雪冰 知乎 编辑:程序博客网 时间:2024/05/16 17:46
package wld.bawei.com.myonemoni2.view;import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;/** * Created by 大金鑫 on 2017/12/3. */public class Liushibuju extends ViewGroup {    public Liushibuju(Context context) {        this(context,null);    }    public Liushibuju(Context context, AttributeSet attrs) {        this(context, attrs,0);    }    public Liushibuju(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 width=0;        int height=0;        int childCount = getChildCount();        for(int k=0;k<childCount;k++){            View childAt = getChildAt(k);            int measuredWidth = childAt.getMeasuredWidth();            int measuredHeight = childAt.getMeasuredHeight();            childAt.layout(width,height,measuredWidth+width,measuredHeight+height);            width+=measuredWidth;            height+=measuredHeight;        }    }}
原创粉丝点击