Android RadioGroup实现自动换行的方法
创始人
2024-11-19 06:02:05
0次

具体内容如下:

public class FlowRadioGroup extends RadioGroup {

 

    private static final String TAG = "MyRadioGroup";

 

    public FlowRadioGroup(Context context) {

        super(context);

    }

 

    public FlowRadioGroup(Context context, AttributeSet attrs) {

        super(context, attrs);

    }

 

    @Override

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthSize = MeasureSpec.getSize(widthMeasureSpec);

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);

        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

 

        //调用ViewGroup的方法,测量子view

        measureChildren(widthMeasureSpec, heightMeasureSpec);

 

        //最大的宽

        int maxWidth = 0;

        //累计的高

        int totalHeight = 0;

 

        //当前这一行的累计行宽

        int lineWidth = 0;

        //当前这行的最大行高

        int maxLineHeight = 0;

        //用于记录换行前的行宽和行高

        int oldHeight;

        int oldWidth;

 

        int count = getChildCount();

        //假设 widthMode和heightMode都是AT_MOST

        for (int i = 0; i < count; i++) {

            View child = getChildAt(i);

            MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();

            oldHeight = maxLineHeight;

            //当前最大宽度

            oldWidth = maxWidth;

 

            int deltaX = child.getMeasuredWidth() + params.leftMargin + params.rightMargin;

            if (lineWidth + deltaX + getPaddingLeft() + getPaddingRight() > widthSize) {//如果折行,height增加

                //和目前最大的宽度比较,得到最宽。不能加上当前的child的宽,所以用的是oldWidth

                maxWidth = Math.max(lineWidth, oldWidth);

                //重置宽度

                lineWidth = deltaX;

                //累加高度

                totalHeight += oldHeight;

                //重置行高,当前这个View,属于下一行,因此当前最大行高为这个child的高度加上margin

                maxLineHeight = child.getMeasuredHeight() + params.topMargin + params.bottomMargin;

                Log.v(TAG, "maxHeight:" + totalHeight + "---" + "maxWidth:" + maxWidth);

 

            } else {

                //不换行,累加宽度

                lineWidth += deltaX;

                //不换行,计算行最高

                int deltaY = child.getMeasuredHeight() + params.topMargin + params.bottomMargin;

                maxLineHeight = Math.max(maxLineHeight, deltaY);

            }

            if (i == count - 1) {

                //前面没有加上下一行的搞,如果是最后一行,还要再叠加上最后一行的最高的值

                totalHeight += maxLineHeight;

                //计算最后一行和前面的最宽的一行比较

                maxWidth = Math.max(lineWidth, oldWidth);

            }

        }

 

        //加上当前容器的padding值

        maxWidth += getPaddingLeft() + getPaddingRight();

        totalHeight += getPaddingTop() + getPaddingBottom();

        setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : maxWidth,

                heightMode == MeasureSpec.EXACTLY ? heightSize : totalHeight);

 

    }

 

    @Override

    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        int count = getChildCount();

        //pre为前面所有的child的相加后的位置

        int preLeft = getPaddingLeft();

        int preTop = getPaddingTop();

        //记录每一行的最高值

        int maxHeight = 0;

        for (int i = 0; i < count; i++) {

            View child = getChildAt(i);

            MarginLayoutParams params = (MarginLayoutParams) child.getLayoutParams();

            //r-l为当前容器的宽度。如果子view的累积宽度大于容器宽度,就换行。

            if (preLeft + params.leftMargin + child.getMeasuredWidth() + params.rightMargin + getPaddingRight() > (r - l)) {

                //重置

                preLeft = getPaddingLeft();

                //要选择child的height最大的作为设置

                preTop = preTop + maxHeight;

                maxHeight = getChildAt(i).getMeasuredHeight() + params.topMargin + params.bottomMargin;

            } else { //不换行,计算最大高度

                maxHeight = Math.max(maxHeight, child.getMeasuredHeight() + params.topMargin + params.bottomMargin);

            }

            //left坐标

            int left = preLeft + params.leftMargin;

            //top坐标

            int top = preTop + params.topMargin;

            int right = left + child.getMeasuredWidth();

            int bottom = top + child.getMeasuredHeight();

            //为子view布局

            child.layout(left, top, right, bottom);

            //计算布局结束后,preLeft的值

            preLeft += params.leftMargin + child.getMeasuredWidth() + params.rightMargin;

 

        }

 

    }

}

相关内容

热门资讯

空调连续工作多久需要停机休息 空调连续工作的时间取决于多个因素,包括空调的型号、功率、使用环境和维护情况等。一般来说,空调可以连续...
空调哪个符号是除湿的 空调除湿功能通常用一个水滴形状的符号表示。不同品牌和型号的空调遥控器可能会有所差异,但一般来说,除湿...
格力空调多长时间加一次氟利昂 1、一般情况下,空调对氟利昂的使用量是非常小的,所以在空调使用了四年左右的时候再考虑加也没问题。2、...
空调用多长时间需要加氟一次 一般情况下是五年。当出现以下这几种情况时,空调才需要加氟。1、空调的室内机和室外机各有两个接口,当接...
格调生活·C标思享荟:卡萨帝在... 9月12日的郑州龙湖金融岛,夜风轻拂,灯光璀璨。“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑...
品牌价值破千亿、高端份额持续领... 9月12日,“C标时刻共传奇——卡萨帝思享荟暨20周年品牌盛典郑州站”在郑州龙湖金融岛举办。2026...
空调主板坏了修800还是换新的 空调主板坏了是修还是换新,需要考虑多个因素,以下是一些建议:1. 维修成本:如果维修费用较低,比如 ...
空调开多久需要休息多长时间 问题:空调开多久需要休息多长时间回答:1、在使用空调的过程中,一般空调开7~8个小时左右需要休息2~...
中央空调多长时间清洗一次 1、一般空调开机前清洗一次即可,空调开机中间时段再清洗一次,空调关机时清洗一次,这样比较合理。2、空...
中央空调氟利昂多久加一次 看空调制热和制冷程度,很多人的观点认为氟利昂必须一年放一次,其实这个观点是错误的,理解上面有点偏差,...