效果图

用法

使用FlowRadioGroup代替RadioGroup

代码

import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.widget.RadioGroup;/** * 流式布局的RadioGroup */public class FlowRadioGroup extends RadioGroup {        public FlowRadioGroup(Context context) {        super(context);    }    public FlowRadioGroup(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int maxWidth = MeasureSpec.getSize(widthMeasureSpec);        int childCount = getChildCount();        int x = 0;        int y = 0;        int row = 0;        for (int index = 0; index < childCount; index++) {            final View child = getChildAt(index);            if (child.getVisibility() != View.GONE) {                child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);                // 此处增加onlayout中的换行判断,用于计算所需的高度                int width = child.getMeasuredWidth();                int height = child.getMeasuredHeight();                x += width;                y = row * height + height;                if (x > maxWidth) {                    x = width;                    row++;                    y = row * height + height;                }            }        }        // 设置容器所需的宽度和高度        setMeasuredDimension(maxWidth, y);    }    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {        final int childCount = getChildCount();        int maxWidth = r - l;        int x = 0;        int y = 0;        int row = 0;        for (int i = 0; i < childCount; i++) {            final View child = this.getChildAt(i);            if (child.getVisibility() != View.GONE) {                int width = child.getMeasuredWidth();                int height = child.getMeasuredHeight();                x += width;                y = row * height + height;                if (x > maxWidth) {                    x = width;                    row++;                    y = row * height + height;                }                child.layout(x - width, y - height, x, y);            }        }    }}

更多相关文章

  1. [转]Android(安卓)开源项目分类汇总
  2. Android(安卓)中获取屏幕长度及宽度
  3. DataBinding(双向绑定)
  4. Android开发(二十九)——layout_weight的含义
  5. android自定义progressbar图片大小自适应
  6. Android中当item数量超过一定大小RecyclerView高度固定
  7. Android(安卓)图片预览模仿朋友圈查看图片效果放大图片,左右滑动
  8. android软键盘弹出引起的各种不适终极解决方案
  9. listview使用小技巧

随机推荐

  1. 关于Android(安卓)properties 中ro开头无
  2. Android开发 之 图片浏览
  3. 一键多渠道打包 只需1秒
  4. Android自定义饼图TTJPieChart
  5. Android(安卓)- AppCompatEditText 重写,
  6. Android(安卓)RatingBar 评分条
  7. android开发环境搭建--android studio安
  8. Android超级终端文件处理命令
  9. Google Android(安卓)开发工程师职位面试
  10. android加载gif图片的动画库