纵行自定义ViewGroup

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

<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);

    }
}
阅读更多
换一批

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