RadioGroup的换行、换行后的均分实现

问题:Radiogroup在横排现实的时候可能超出屏幕宽度

方法1:为Radiogroup添加 ScrollView()

方法2:使Radiogroup换行;

方法一很简单这里就不说了,主要说一下方法2:

考虑到要为Radiogroup要换行所以要使用到布局方法:onLayout();

和测量方法:onMeasure();当然还要继承Radiogroup的所有属性。

1.在从写onMeasure方法时要注意有:

a、Radiogroup的子个数

b、子View的宽和高;

c、判断子view是否显示;

2.onLayout在这里主要是确定是否要换行注意逻辑就行了。

不多说直接看代码:

/**
 * Created by Hear on 2016/5/6.
 */
public class MyRadioGroup extends RadioGroup{
    public MyRadioGroup(Context context) {
        this(context, null);
    }

    public MyRadioGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    /**
     * 复写测量的方法
     *
     * @param widthMeasureSpec  宽度
     * @param heightMeasureSpec 高度
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //获取到孩子的个数
        int childCount = getChildCount();
        int x = 0;
        int y = 0;
        int row = 0;
        //获取到最大宽度
        int maxWidth = View.MeasureSpec.getSize(widthMeasureSpec);


        //遍历孩子,并对它们进行测量
        for (int i = 0; i < childCount; i++) {
            //获取到孩子
            View child = getChildAt(i);
            if (child.getVisibility() != View.GONE) {

                child.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
                int width = child.getMeasuredWidth();
                int height = child.getMeasuredHeight(); //计算出大小
                y = height * (row + 1);
                x += width;

                if (x > maxWidth) {
                    row++;
                    //如果大于了最大的宽度,那么就换行咯
                    x = width;
                    y = height * (row + 1) + height / 2;
                }
            }
            setMeasuredDimension(maxWidth, y);
            Log.d("message", "y = " + y);
        }
    }

    /**
     * 复写布局方法
     *
     * @param changed 是否改变
     * @param l       左
     * @param t       上
     * @param r       右
     * @param b       下
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //获取到孩子的个数
        int childCount = getChildCount();
        int x = 0;
        int y = 0;
        int row = 0;
        //间隙view之间
        int weight = 50;

        //计算最大宽度
        int maxWidth = r - l;

        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() != View.GONE) {
                int width = child.getMeasuredWidth();
                int height = child.getMeasuredHeight();
                //计算XY
                x += width+width/3;
                if (row!=0){
                    y = (row + 1) * height+height/2;
                }else {
                    y = (row + 1) * height;
                }
                if (x > maxWidth) {
                    //换行
                    row++;
                    x = width+width/3;
                    y = (row + 1) * height+height/2;
                }
                //设置孩子的位置
                child.layout(x - width, y - height, x, y);
            }
        }

    }
}


ScrollView



你可能感兴趣的:(view)