栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

自定义ViewGroup

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

自定义ViewGroup

View 视图树结构

Activity --> PhoneWindow --> DecorView

重要类:

LayoutParams : 布局参数,子View通过LayoutParams告诉父容器(ViewGroup)应该如何放置自己。

MeasureSpec:父容器对 view 的布局上的限制;

View ViewGroup 关系图:

自定义ViewGroup 关键是要重写 onMeasure onLayout 方法:

onMeasure 测量过程一般先测量子View,再测量自己;

测量子View:

        测量子View需要的WidthMeasureSpec:widthMeasureSpec 、paddingLeft + paddingRight  自己需要的尺寸;width 子view尺寸、

                int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, paddingLeft + paddingRight , width);

                int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, paddingTop + paddingBottom , height);

        测量子View:

                childView.measure(childWidthMeasureSpec, childHeightMeasureSpec);

        测量阶段获取View 尺寸:

                int childHeight = childView.getMeasuredHeight();

                int childWidth = childView.getMeasuredWidth();

        测量结束后,按照需要布局保存测量结果;

测量自己:

        根据测量模式,将自己的尺寸设置

        int realWidth = (widthMode == MeasureSpec.EXACTLY) ? widthSize:                 parentNeededWidth;
        int realHeight = (heightMode == MeasureSpec.EXACTLY) ? heightSize:                 parentNeededHeight;
        setMeasuredDimension(realWidth, realHeight);

布局view onLayout,将View 布局在界面,遍历所有View,将获取的坐标设置,完成布局

view.layout(left,top,right,bottom);

补充:MeasureMode 对,测量结果的影响,见

https://www.jianshu.com/p/2fffa71ac5ba
https://blog.csdn.net/qq_40881680/article/details/82378452 https://blog.csdn.net/sinat_29874521/article/details/79994169

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/694982.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号