Android 继承ViewGroup必须重写onMeasure方法和onLayout方法
发布日期:2021-06-30 22:35:04
浏览次数:4
分类:技术文章
本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年05月05日 18时41分52秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
在滴滴和字节跳动干了 2 年后端开发,太真实…
2019-05-01
提高github下载速度的方法「100%有效」可达到2MB/s
2019-05-01
手工测试10k见顶了,自动化测试有多少?
2019-05-01
25个常用Matplotlib图的Python代码,收藏收藏!
2019-05-01
微软官方再次上线了Python教程,这次比较高级
2019-05-01
最佳垃圾代码,十九条关键准则
2019-05-01
刺激!一行代码就可下载b站等全网视频!
2019-05-01
早知道这些免费 API,我就可以不用到处爬数据了!
2019-05-01
5个方法,教你用Python赚外快!
2019-05-01
如何画出优秀的架构图
2019-05-01
潘石屹Python考试成绩 99 分,是什么水平?
2019-05-01
新手如何有效的刷算法题(LeetCode)
2019-05-01
微信真正的暗黑模式来袭!内附下载地址
2019-05-01
学Python两年还是没入门,肿么办?
2019-05-01
字符串驻留是个什么鬼!
2019-05-01
再见,print
2019-05-01
Pandas进阶大神!从0到100你只差这篇文章!
2019-05-01
别再乱用 try except 了...
2019-05-01
灵魂三拷问:你真的了解HTTPS吗
2019-05-01
一行代码能实现哪些丧心病狂的功能
2019-05-01