转:(22条消息) RadioGroup 自动换行且保留点击事件_再见孙悟空_的博客-CSDN博客

public class MyRadioGroup extends RadioGroup {
private static final String TAG = "RadioGroupEx";

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

public MyRadioGroup(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. java 打印流(PrintStream)
  2. 通过Ajax异步提交的方法实现从数据库获取省份和城市信息实现二级联动(xml方法)
  3. 管理我的数据之GDBM
  4. Android中常用的布局
  5. Go文件操作
  6. RocketMQ消费者示例程序
  7. 怎样做出优秀的扁平化设计风格 PPT 或 Keynote 幻灯片演示文稿?(装)
  8. javascript的排序算法
  9. Codeforces Round #232 (Div. 1)
  10. [翻译]ASP.NET Web API 2入门
  11. 手机端MVC-js框架-Gillie-中文版本
  12. PHP备份数据库的原理和方法 57
  13. android离线下载的相关知识
  14. 十个 PHP 开发者最容易犯的错误
  15. Spring-cloud (八) Hystrix 请求缓存的使用
  16. Delphi 字符串转日期,强大到窒息,VarToDateTime 解决了 困扰很久的小问题
  17. js深度复制三种方法
  18. bash 3
  19. 1259 整数划分 V2
  20. webpack学习笔记--配置总结

热门文章

  1. MySQL事务(四大特征)-存储过程
  2. Navicat Premium无法连接到oracle数据库的解决方法
  3. 第四篇:前端之BOM与DOM
  4. 13、IDEA 提取代码块成独立方法 Extract Method
  5. 【转载】EXCEL VBA 中的Range.offset和Range.resize
  6. 记一个难以发现的 UB
  7. MongoDB6.0的安装「2023年」
  8. vs code .net core Linux下离线安装Nuget包
  9. Nacos详解
  10. MySQL 判断语句 条件函数 case when、if、ifnull