自定义viewGroup(3)


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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值