自定义ViewGroup-通过内边距加深理解

来源:互联网 发布:网络神豪系统txt下载 编辑:程序博客网 时间:2024/06/05 14:32
 读取 4 个方向的
 padding:

 public int getPaddingLeft () 离左边的 padding

 public int getPaddingRight () 离右边的 padding

 public int getPaddingTop () 离顶部的 padding

 public int getPaddingRight () 离底部的 padding


CustomViewGroup3:

public class CustomViewGroup3 extends ViewGroup {    public CustomViewGroup3(Context context) {        super(context);    }    public CustomViewGroup3(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public CustomViewGroup3(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        //测量所有的子View        measureChildren(widthMeasureSpec, heightMeasureSpec);        //测量自己的宽高        int width = measureWidth(widthMeasureSpec);        int height = measureHeight(heightMeasureSpec);        setMeasuredDimension(width, height);    }    private int measureHeight(int widthMeasureSpec) {        //父控件建议自己测量的值        int measureMode = MeasureSpec.getMode(widthMeasureSpec);        int measureSize = MeasureSpec.getSize(widthMeasureSpec);        int width = 0;        if (measureMode == MeasureSpec.EXACTLY) {            width = measureSize;        } else if (measureMode == MeasureSpec.AT_MOST) {            int aWidth = 0;            int bWidth = 0;            int cWidth = 0;            int dWidth = 0;            for (int i = 0; i < getChildCount(); i++) {                if (i == 0) {                    aWidth = getChildAt(i).getMeasuredWidth();//左边View的宽度                } else if (i == 1) {                    bWidth = getChildAt(i).getMeasuredWidth();                } else if (i == 2) {                    cWidth = getChildAt(i).getMeasuredWidth();                } else if (i == 3) {                    dWidth = getChildAt(i).getMeasuredWidth();                }            }            //getPaddingTop:top离top的内边距   getPaddingBottom:bottom离底的内边距            width = Math.max(aWidth, bWidth) + Math.max(cWidth, dWidth)                    + getPaddingTop() + getPaddingBottom();//取到宽度的最大值        }        return width;    }    private int measureWidth(int heightMeasureSpec) {        int measureMode = MeasureSpec.getMode(heightMeasureSpec);        int measureSize = MeasureSpec.getSize(heightMeasureSpec);        int height = 0;        if (measureMode == MeasureSpec.EXACTLY) {            height = measureSize;        } else if (measureMode == MeasureSpec.AT_MOST) {            int aHeight = 0;            int bHeight = 0;            int cHeight = 0;            int dHeight = 0;            for (int i = 0; i < getChildCount(); i++) {                if (i == 0) {                    aHeight = getChildAt(i).getMeasuredHeight();                } else if (i == 1) {                    bHeight = getChildAt(i).getMeasuredHeight();                } else if (i == 2) {                    cHeight = getChildAt(i).getMeasuredHeight();                } else if (i == 3) {                    dHeight = getChildAt(i).getMeasuredHeight();                }            }            height = Math.max(aHeight, bHeight) + Math.max(cHeight, dHeight)                    + getPaddingLeft() + getPaddingRight();//取到宽度的最大值        }        return height;    }    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {        int leftPadding = getPaddingLeft();        int rightPadding = getPaddingRight();        int topPadding = getPaddingTop();        int bottomPadding = getPaddingBottom();        for (int i = 0; i < getChildCount(); i++) {            View childView = getChildAt(i);//某一个子View            if (i == 0) {                childView.layout(leftPadding,                        topPadding,                        childView.getMeasuredWidth() + leftPadding,                        childView.getMeasuredHeight() + topPadding);            } else if (i == 1) {                childView.layout(getMeasuredWidth() - childView.getMeasuredWidth() - rightPadding,                        topPadding,                        getMeasuredWidth() - rightPadding,                        childView.getMeasuredHeight() + topPadding);            } else if (i == 2) {                childView.layout(leftPadding,                        getMeasuredHeight() - childView.getMeasuredHeight() - bottomPadding,                        childView.getMeasuredWidth() + leftPadding,                        getMeasuredHeight() - bottomPadding);            } else if (i == 3) {                childView.layout(getMeasuredWidth() - childView.getMeasuredWidth() - rightPadding,                        getMeasuredHeight() - childView.getMeasuredHeight() - bottomPadding,                        getMeasuredWidth() - rightPadding,                        getMeasuredHeight() - bottomPadding);            }        }    }}

activity_main:

<com.example.user.myapplication4.CustomViewGroup3        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="#FFCCCCCC"        android:paddingBottom="15dp"        android:paddingLeft="20dp"        android:paddingRight="30dp"        android:paddingTop="10dp">        <TextView            android:layout_width="100dp"            android:layout_height="100dp"            android:background="@android:color/holo_blue_bright" />        <TextView            android:layout_width="100dp"            android:layout_height="100dp"            android:background="@android:color/holo_blue_dark" />        <TextView            android:layout_width="100dp"            android:layout_height="100dp"            android:background="@android:color/holo_red_dark" />        <TextView            android:layout_width="100dp"            android:layout_height="100dp"            android:background="@android:color/holo_green_light" /></com.example.user.myapplication4.CustomViewGroup3>

结果展示:



0 0
原创粉丝点击