自动换行_Android-自动换行的标签列表-采用RadioGroup实现-多行可以扩展折叠收起...

这篇博客介绍了如何在Android中使用RadioGroup实现自动换行的标签列表,解决了高度显示不全的问题。提供了一个名为LineWrapRadioGroup.java的代码示例,并提醒读者代码已更新修复了间距和高度计算的错误。博主祝福读者新年快乐。
摘要由CSDN通过智能技术生成

直接上代码:

LineWrapRadioGroup.java - 有需求或问题可以自行完善的,有问题可以私信

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RadioGroup;

/**
 * 自动换行的RadioGroup
 */
public class LineWrapRadioGroup extends RadioGroup {
    private static final String TAG = "RadioGroup";

    //@hl 记录是第几行,需要做如果是第三行开始,则需要开始隐藏这些View
    private static final int MAX_SHOW_COUNT = 6;
    private int line_cout = 0;
    private boolean bFirtLine = true;
    private int totalShowHeight, totalFolderShowHeight = 0;
    //@hl 控制是否显示所有
    private boolean bShowAll = false;

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

    public LineWrapRadioGroup(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;

                //@hl 两行之后只显示两行的高度,不再增加换行高度
                ++line_cout;
                if (line_cout <= MAX_SHOW_COUNT) {
                    totalFolderShowHeight = totalHeight;
                }
            } else {
                //不换行,累加宽度
                lineWidth += deltaX;
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值