纵行自定义ViewGroup


<example.com.viewgroup.MyViewGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="#aa55cc">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"

        android:text="AAAA" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"

        android:text="BBBB" />
</example.com.viewgroup.MyViewGroup>
 
ackage example.com.viewgroup;

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

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

public class MyViewGroup extends ViewGroup {

    //自定义top和中间间距间距
    private int marginCenter = 10;
    private int marginTop = 50;

    public MyViewGroup(Context context) {
        super(context);
    }

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

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

    //测量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int viewHeight = 0;
        int viewWidth = 0;
        //测量ViewGroup
        measureChildren(widthMeasureSpec, heightMeasureSpec);
        //获取测量的宽高度的模式和大小
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);//就是在xml里面定义的宽和高
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        //判断高的moshi
        switch (modeHeight) {
            //计算自定义view的高度,自定义高度=view高度累加+间距
            case MeasureSpec.AT_MOST:
                //遍历子view的数量
                for (int i = 0; i < getChildCount(); i++) {
                    View childAt = getChildAt(i);
                    //得到子宽度和高度
                    int height = childAt.getMeasuredHeight();
                    viewHeight += height + marginCenter;
                }
                viewHeight += 50 * 2 - marginCenter;
                break;

            case MeasureSpec.EXACTLY:
                viewHeight = sizeHeight;

                break;


        }
        //自定义宽
        switch (modeWidth) {
            case MeasureSpec.AT_MOST:
                View childAt = getChildAt(0);
                int width = childAt.getMeasuredWidth();
                viewWidth = width + 50 * 2;
                break;
            case MeasureSpec.EXACTLY:
                viewWidth = sizeWidth;
                break;

        }
        setMeasuredDimension(viewWidth, viewHeight);
    }

    //布局
    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        int marginLeft = 50;
        int ftop = marginTop;
        int fcenter = marginCenter;

        //遍历子view的数量
        for (int j = 0; j < getChildCount(); j++) {
            View childAt = getChildAt(j);
            //得到宽和高
            int height = childAt.getMeasuredHeight();
            int width = childAt.getMeasuredWidth();

            childAt.layout(marginLeft, ftop, marginLeft + width, ftop + height);
            ftop += fcenter + height;
        }

    }


    //大小改变
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值