1,获取长宽尺寸(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);

2,测量子view

//调用ViewGroup的方法,测量子viewmeasureChildren(widthMeasureSpec, heightMeasureSpec);

3,计算宽度

//最大的宽int maxWidth = 0;//累计的高int totalHeight = 0;//当前这一行的累计行宽int lineWidth = 0;//当前这行的最大行高int maxLineHeight = 0;//用于记录换行前的行宽和行高int oldHeight;int oldWidth;int count = getChildCount();//假设 widthMode和heightMode都是AT_MOSTfor (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的宽,所以用的是oldWidthmaxWidth = Math.max(lineWidth, oldWidth);//重置宽度lineWidth = deltaX;//累加高度totalHeight += oldHeight;//重置行高,当前这个View,属于下一行,因此当前最大行高为这个child的高度加上marginmaxLineHeight = child.getMeasuredHeight() + params.topMargin + params.bottomMargin;} 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);

4,重新布局

  @Overrideprotected 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;}}

Android 的自定义RadioGroup相关推荐

  1. android自定义radiogroup,Android自定义RadioGroup

    最近做项目时需要用到RadioGroup,发现Android原生的RadioGroup太丑了,所以自己写了一个,效果如下所示: 其实就是由4个Button组成的LinearLayout,只是为了方便点 ...

  2. android自定义radiogroup,Android 自定义View实现任意布局的RadioGroup效果

    前言 RadioGroup是继承LinearLayout,只支持横向或者竖向两种布局.所以在某些情况,比如多行多列布局,RadioGroup就并不适用 . 本篇文章通过继承RelativeLayout ...

  3. android自定义radiogroup,自定义RadioGroup

    自定义RadioGroup 在Android系统中,自带的RadioGroup只能指定横向和纵向两种布局,所以有的时候我们需要自定义RadioGroup. 首先分析一下,就是在系统自带的RadioGr ...

  4. Android ListView 自定义背景后 滚动时的背景变黑问题

    ListView是常用的显示控件,默认背景是和系统窗口一样的透明色,如果给ListView加上背景图片,或者背景颜色时,滚动时listView会黑掉,原因是,滚动时,列表里面的view重绘时,用的依旧 ...

  5. android 实现自定义监听接口,Android在自定义类中实现自定义监听器方式

    Android在自定义类中实现自定义监听器方式 发布时间:2020-08-31 06:19:39 来源:脚本之家 阅读:203 作者:Simon_Qi 监听器可以说是Android开发中最常用的东西之 ...

  6. android sqlite自定义函数,Android中自定义一个View的方法详解

    本文实例讲述了Android中自定义一个View的方法.分享给大家供大家参考,具体如下: Android中自定义View的实现比较简单,无非就是继承父类,然后重载方法,即便如此,在实际编码中难免会遇到 ...

  7. android+自定义皮肤,android studio自定义更换皮肤详细图文教程

    android studio这款app程序开发软件内也内置了多种皮肤主题,程序开发人员如果感觉一种皮肤太过单调乏味,可以选择使用软件内的其他皮肤风格,软件默认的皮肤是IntelliJ,还有黑色的Dra ...

  8. android 自定义搜索框edittext,Android编程自定义搜索框实现方法【附demo源码下载】...

    本文实例讲述了Android编程自定义搜索框实现方法.分享给大家供大家参考,具体如下: 先来看效果图吧~ 分析:这只是模拟了一个静态数据的删除与显示 用EditText+PopupWindow+lis ...

  9. Android Studio自定义视图无法预览

    Android Studio自定义视图没有办法预览 我想大家应该都和我一样,如果看到布局的编码的时候如果右边能够非常直观地显示出对应的视图,心里会非常舒心,像官方提供的tools命名空间就是为了这个目 ...

最新文章

  1. centeros7网络服务无法启动_Linux网络服务02——DHCP原理与配置
  2. java been 字段命名的坑
  3. 《系统集成项目管理工程师》必背100个知识点-73配置管理和变更管理
  4. 求【javascript设计模式】【高性能网站建设指南】PDF!哪位有给下啊!!!
  5. flask ajax 文件上传,python flask使用ajax请求上载文件。文件为空
  6. vi/vim使用进阶: 文件浏览和缓冲区浏览
  7. CSS3贝塞尔曲线$$动画效果
  8. 视觉SLAM十四讲学习笔记-第五讲-图像和实践
  9. fillna填充某一列_DataFrame基础运算以及空值填充的案例分析
  10. Katana 项目入门
  11. CorelDRAW如何设置填充颜色和边框颜色
  12. c语言fabs函数的返回值,fabs()函数
  13. Matplotlib中的“plt”和“ax”到底是什么?
  14. Nessus8.15安装不限IP(win10)
  15. 周末作业——循环练习题
  16. 生动化你的表达——DuerOS中的SSML应用
  17. 深富策略:新的动力正在酝酿 重点布局三条主线
  18. 7-3 水仙花数(20 分) (20 分)(PTA Python版本)
  19. 如何删除流氓弹窗广告(全民仙战)
  20. 鼎捷ERP二次开发教程 Tiptop GP开发资料大全 Tipto开发实战经验 鼎捷开发实战例子 Tiptop GP二次开发项目例子 4GL开发Demo 鼎捷二次开发完整例子 鼎捷ERP二次开发入门

热门文章

  1. android+jacoco多模块项目中统计子模块代码覆盖率
  2. asp.net977-汽车租赁管理系统#毕业设计
  3. AMD 锐龙CPU安装genymotion的问题
  4. (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG)
  5. 拖动鼠标移动文件时是复制操作还是移动操作判断依据
  6. Your browser sent a request that this server could not understand. Additionally, a 400 Bad Request e
  7. 华为手机显示高德位置服务器,华为手机用户尝鲜!高德地图上线车道级导航:高精度还原真实道路场景...
  8. 【数据库MySQL】-- 视图的作用和使用
  9. bind函数失败linux,为什么bind函数返回-1(绑定失败)?
  10. 快速启动软件 Listarty