自定义viewGroup(3)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/niu_yue_jiao/article/details/79952665

ackage example.com.attrsviewgroup;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * author:Created by niuyuejiao on 2018/4/14.
 */

public class MyViewGroup extends ViewGroup {


    private int orientation = 0;
    int marignTop = 30;//上边距
    int marignVerCenter = 10;//横行时中间距离
    int marignHorCenter = 10;//纵行时中间
    int marignLeft = 20;//左边距


    public MyViewGroup(Context context) {
        this(context, null);
    }


    public MyViewGroup(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inintView(context, attrs, defStyleAttr);
    }

    private void inintView(Context context, AttributeSet attrs, int defStyleAttr) {
        //或得样式属性
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyViewGroup);
    //得到属性值,默认是0(纵行的)
    orientation = typedArray.getInt(R.styleable.MyViewGroup_ori, 1);

}

    //测量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //定义一个总的宽和高
        int totalHeight = 0;
        int totalWidth = 0;
        //遍历子view的数量
        for (int i = 0; i < getChildCount(); i++) {
            View childAt = getChildAt(i);
            measureChild(childAt, widthMeasureSpec, heightMeasureSpec);
            //判断
            if (orientation == 0) {//纵行0
                totalHeight += childAt.getMeasuredHeight() + marignVerCenter;
                totalWidth = childAt.getMeasuredWidth() + marignLeft * 2;
            } else if (orientation == 1) {//横行1
                totalHeight = childAt.getMeasuredHeight() + marignTop * 2;
                totalWidth += childAt.getMeasuredWidth() + marignHorCenter;
            } else if (orientation == 2) {//all
                totalHeight += childAt.getMeasuredHeight() + marignVerCenter;
                totalWidth += childAt.getMeasuredWidth() + marignHorCenter;
            }
        }
        //模式
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        //宽高
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);

        if (orientation == 0) {//纵行0
            switch (modeHeight) {
                case MeasureSpec.AT_MOST:
                    totalHeight += marignTop * 2 - marignVerCenter;
                    break;
                case MeasureSpec.EXACTLY:
                    totalHeight = sizeHeight;
                    break;

            }
        } else if (orientation == 1) {//横行1
            switch (modeWidth) {
                case MeasureSpec.AT_MOST:
                    totalWidth += marignLeft * 2 - marignHorCenter;
                    break;
                case MeasureSpec.EXACTLY:
                    totalWidth = sizeWidth;
                    break;

            }
        } else if (orientation == 2) {//all
            switch (modeHeight) {
                case MeasureSpec.AT_MOST:
                    totalHeight += marignTop * 2;
                    totalWidth += marignLeft * 2;
                    break;
                case MeasureSpec.EXACTLY:
                    totalWidth = sizeWidth;
                    break;
            }
            //totalHeight += marignTop;
        }

        setMeasuredDimension(totalWidth, totalHeight);
    }

    //布局
    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        int mLeft = marignLeft;//左边距
        int mTop = marignTop;//上边距
        int mHorCenter = marignHorCenter;//两个子view横行中间的间距
        int mVerCenter = marignVerCenter;//两个子view纵行中间的间距
        //遍历子view的数量
        for (int j = 0; j < getChildCount(); j++) {
            View childAt = getChildAt(j);
            //获取子view的宽和高
            int measuredHeight = childAt.getMeasuredHeight();
            int measuredWidth = childAt.getMeasuredWidth();
            childAt.layout( mLeft, mTop, mLeft + measuredWidth, mTop + measuredHeight);
            if (orientation == 0) {//                mTop += measuredHeight + mVerCenter;
            } else if (orientation == 1) {//                mLeft += measuredWidth + mHorCenter;
            } else if (orientation == 2) {//all
                mTop += measuredHeight + mVerCenter;
                mLeft += measuredWidth + mHorCenter;
            }
        }
    }
}
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="MyViewGroup">
        <attr name="orientation" format="integer" />
        <attr name="ori" format="enum">
            <enum name="vertical" value="0" />
            <enum name="horizontal" value="1" />
            <enum name="all" value="2" />
        </attr>

    </declare-styleable>
</resources>
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页