超出内容就自动换行的RadioGroup

这篇博客介绍了如何自定义一个RadioGroup,使其在内容超出屏幕宽度时自动换行。作者通过修改onMeasure()方法,测量子View大小并进行换行处理。换行策略是调整子View的margin,而不是使用LinearLayout。文章中还提到,这种方式无法保留子View原有的margin,作者期待有解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

超出内容就自动换行的RadioGroup


效果图:
超出内容就自动换行的RadioGroup

步骤

  1. 自定义RadioGroup
  2. 改写onMeasure()

原理

  • 测量子view的大小,如果单行宽度超出屏幕宽度,则将这个子view做换行处理。
  • 这里用到的换行方式可能和别人不同,网上的大部分代码都是在RadioGroup中嵌入多行LinearLayout,这里用的方式是改变子view的margin。
  • 换行处理就是将该view的marginTop变成上一行的高度,marginLeft变成负的,具体数值是上一行的宽度。ps:只需要将每行第一个设置marginLeft。

还有些话:
这种方法无法保留子view在xml中原有的margin,原因是我没找到在代码中获取margin的方法,如果有大神知道,希望能留言告诉我。

下面就是代码了。

代码

public class CustomRadioGroup extends RadioGroup {
    public CustomRadioGroup(Context context) {
        super(context);
    }

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

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
    }

    @Override
    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        super.setOnCheckedChangeListener(listener);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int lineWidth = 0;//用于记录宽度是否超出,超出时归零
        int totalLineHeight = 0;//用于记录已有内容的高度,用于放置下一行时使用
        int lineHeight = 0;//用于记录已有内容的高度,用于放置下一行时使用

        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            measureChild(child,widthMeasureSpec, heightMeasureSpec);
            int factWidth = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();//radioGroup中可用的宽度
            if ((lineWidth + child.getMeasuredWidth()+10) > factWidth) {
                lineHeight = totalLineHeight;
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(child.getLayoutParams());
                params.setMargins( -lineWidth, lineHeight, 0, 0);
                child.setLayoutParams(params);
                totalLineHeight += child.getMeasuredHeight();
                lineWidth = 0;
                lineWidth += child.getMeasuredWidth();
            } else {
                lineWidth += child.getMeasuredWidth();
                int childBottom = (int) (child.getY() + child.getMeasuredHeight());
                if (totalLineHeight < childBottom) {
                    totalLineHeight = childBottom;
                }
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(child.getLayoutParams());
                if(lineHeight == 0) {
                    params.setMargins(0, lineHeight, 0, 0);
                }else {
                    params.setMargins(10, lineHeight, 0, 0);
                    lineWidth+=10;
                }
                child.setLayoutParams(params);
            }
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值