Android 继承ViewGroup必须重写onMeasure方法和onLayout方法
发布日期:2021-06-30 22:35:04
浏览次数:3
分类:技术文章
本文共 2978 字,大约阅读时间需要 9 分钟。
1.在方法onMeasure中调用setMeasuredDimension方法
setMeasuredDimension(int measuredWidth, int measuredHeight) 在onMeasure(int, int)中,必须调用setMeasuredDimension(int width, int height)来存储测量得到的宽度和高度值,如果没有这么去做会触发异常IllegalStateException。2.在方法onMeasure中调用孩子的measure方法
measure(int widthMeasureSpec, int heightMeasureSpec) 这个方法用来测量出view的大小。父view使用width参数和height参数来提供constraint信息。实际上,view的测量工作在onMeasure(int, int)方法中完成。因此,只有onMeasure(int, int)方法可以且必须被重写。参数widthMeasureSpec提供view的水平空间的规格说明,参数heightMeasureSpec提供view的垂直空间的规格说明。—————————————————–
(java 架构师全套教程,共760G, 让你从零到架构师,每月轻松拿3万) 请先拍 , 下载请用百度盘 目录如下: 01.高级架构师四十二个阶段高 02.Java高级系统培训架构课程148课时 03.Java高级互联网架构师课程 04.Java互联网架构Netty、Nio、Mina等-视频教程 05.Java高级架构设计2016整理-视频教程 06.架构师基础、高级片 07.Java架构师必修linux运维系列课程 08.Java高级系统培训架构课程116课时 (送:hadoop系列教程,java设计模式与数据结构, Spring Cloud微服务, SpringBoot入门) 01高级架构师四十二个阶段高内容: 01高级架构师四十二个阶段高内容: —————————————————–protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); setMeasuredDimension(width, height); for (int i = 0; i < getChildCount(); i++) { getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec); }}
3.解析onLayout(boolean, int, int, int, int)方法
onLayout(boolean changed, int l, int t, int r, int b)调用场景:在view给其孩子设置尺寸和位置时被调用。子view,包括孩子在内,必须重写onLayout(boolean, int, int, int, int)方法,并且调用各自的layout(int, int, int, int)方法。
参数说明:参数changed表示view有新的尺寸或位置;参数l表示相对于父view的Left位置;参数t表示相对于父view的Top位置;参数r表示相对于父view的Right位置;参数b表示相对于父view的Bottom位置。.
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int cWidth = 0; int cHeight = 0; MarginLayoutParams params = null; for (int i = 0; i < getChildCount(); i++) { View childView = getChildAt(i); cWidth = childView.getMeasuredWidth(); cHeight = childView.getMeasuredHeight(); params = (MarginLayoutParams) childView.getLayoutParams(); int cl = 0; int ct = 0; int cr = 0; int cb = 0; switch (i) { case 0: cl = params.leftMargin; ct = params.topMargin; break; case 1: cl = getWidth() - cWidth - params.leftMargin - params.rightMargin; ct = params.topMargin; break; case 2: cl = params.leftMargin; ct = getHeight() - cHeight - params.topMargin - params.bottomMargin; break; case 3: cl = getWidth() - cWidth - params.leftMargin - params.rightMargin; ct = getHeight() - cHeight - params.topMargin - params.bottomMargin; break; } cr = cl + cWidth; cb = ct + cHeight; childView.layout(cl, ct, cr, cb); } }
转载地址:https://lovoo.blog.csdn.net/article/details/51226884 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2024年04月27日 23时41分10秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
别再问我什么是A/B测试了!
2019-04-30
如何用同期群分析模型提升留存?(Tableau实战)
2019-04-30
爱了,吹爆这个高颜值的流程图工具!
2019-04-30
一个数据项目
2019-04-30
基于JAVA_JSP电子书下载系统
2019-04-30
基于java出租车计价器设计与实现
2019-04-30
十二时辰篇:这该死的 996
2019-04-30
2021最新 上海互联网公司排名
2019-04-30
字节vs快手!取消大小周之战
2019-04-30
送一个闲置显示器!
2019-04-30
Oracle 行转列 pivot函数基本用法
2019-04-30
Oracle字符串分隔符替换(替换奇数个或偶数个)
2019-04-30
Oracle 利用 UTL_SMTP 包发送邮件
2019-04-30
Oracle 的循环中的异常捕捉和处理
2019-04-30
Oracle通过pivot和unpivot配合实现行列转换
2019-04-30
给Oracle数据库换一个1522端口的监听
2019-04-30
Excel表格数据生成ECharts图表
2019-04-30
阿里云短信服务python版,pyinstaller打包运行时缺少文件
2019-04-30
Oracle的pfile和spfile的一点理解和笔记
2019-04-30