Android 中MeasureSpec的创建规则

article/2025/8/25 23:05:44

概述

在Android中,View的onMeasure()方法用来对控件进行测量,确定控件的宽高。该方法的两个参数widthMeasureSpec和heightMeasureSpec由父View计算后传入子view的measure()方法,再由子view的measure()方法传入onMeasure()方法,本文将介绍MeasureSpec的创建规则

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

LayoutParams

在开始源码分析前,我们要先介绍下LayoutParams,因为子view的MeasureSpec的创建需要用到子view的LayoutParams,LayoutParams有如下三种类型

  • FILL_PARENT/MATCH_PARENT:填满父View
  • WRAP_CONTENT:包裹内容
  • 确定值:确定的值

源码分析

首先我们要先找到父View中调用子view的measure()方法的入口

首先先看View类的onMeasure方法如下,由于View是所有控件的基类,这里只是一个默认实现

我们应该看得是ViewGroup类型的类的onMeasure方法

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));}

查看源码后发现ViewGroup.java类中并未重写onMeasure()方法,且ViewGroup类是个抽象类,所以我们应该把目光投向ViewGroup的子类,比如AbsoluteLayout、LinearLayout、FrameLayout等

我们选其中一个看看,查看AbsoluteLayout的onMeasure()方法,可以知道如下的调用栈

可以看到measureChildren()和measureChild()是定义在ViewGroup中的,在子类中可以访问

AbsoluteLayout.onMeasure()->ViewGroup.measureChildren()->ViewGroup.measureChild()

接下来直接看measureChild()的源码

由下面的源码及其注释可以看到,子view的MeasureSpec是交由getChildMeasureSpec()方法来计算,终于是让我们找到了

    /*** Ask one of the children of this view to measure itself, taking into* account both the MeasureSpec requirements for this view and its padding.* The heavy lifting is done in getChildMeasureSpec.** @param child The child to measure* @param parentWidthMeasureSpec The width requirements for this view* @param parentHeightMeasureSpec The height requirements for this view*/protected void measureChild(View child, int parentWidthMeasureSpec,int parentHeightMeasureSpec) {final LayoutParams lp = child.getLayoutParams();final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,mPaddingLeft + mPaddingRight, lp.width);final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,mPaddingTop + mPaddingBottom, lp.height);child.measure(childWidthMeasureSpec, childHeightMeasureSpec);}

接下来我们就看看getChildMeasureSpec()方法的源码,也是本文的重点内容

该方法的功能就是通过父view的MeasureSpec和子View的LayoutParams来计算出子View的MeasureSpec

父View的MeasureSpec有MeasureSpec.EXACTLY、MeasureSpec.AT_MOST和MeasureSpec.UNSPECIFIED三种类型,而子view的LayoutParams也有MATCH_PARENT、WRAP_CONTENT或者确定值三种类型,3*3得出9种情况,如下代码所示

    /*** Does the hard part of measureChildren: figuring out the MeasureSpec to* pass to a particular child. This method figures out the right MeasureSpec* for one dimension (height or width) of one child view.** The goal is to combine information from our MeasureSpec with the* LayoutParams of the child to get the best possible results. For example,* if the this view knows its size (because its MeasureSpec has a mode of* EXACTLY), and the child has indicated in its LayoutParams that it wants* to be the same size as the parent, the parent should ask the child to* layout given an exact size.** @param spec The requirements for this view* @param padding The padding of this view for the current dimension and*        margins, if applicable* @param childDimension How big the child wants to be in the current*        dimension* @return a MeasureSpec integer for the child*/public static int getChildMeasureSpec(int spec, int padding, int childDimension) {int specMode = MeasureSpec.getMode(spec);int specSize = MeasureSpec.getSize(spec);int size = Math.max(0, specSize - padding);int resultSize = 0;int resultMode = 0;switch (specMode) {// Parent has imposed an exact size on uscase MeasureSpec.EXACTLY:if (childDimension >= 0) {resultSize = childDimension;resultMode = MeasureSpec.EXACTLY;} else if (childDimension == LayoutParams.MATCH_PARENT) {// Child wants to be our size. So be it.resultSize = size;resultMode = MeasureSpec.EXACTLY;} else if (childDimension == LayoutParams.WRAP_CONTENT) {// Child wants to determine its own size. It can't be// bigger than us.resultSize = size;resultMode = MeasureSpec.AT_MOST;}break;// Parent has imposed a maximum size on uscase MeasureSpec.AT_MOST:if (childDimension >= 0) {// Child wants a specific size... so be itresultSize = childDimension;resultMode = MeasureSpec.EXACTLY;} else if (childDimension == LayoutParams.MATCH_PARENT) {// Child wants to be our size, but our size is not fixed.// Constrain child to not be bigger than us.resultSize = size;resultMode = MeasureSpec.AT_MOST;} else if (childDimension == LayoutParams.WRAP_CONTENT) {// Child wants to determine its own size. It can't be// bigger than us.resultSize = size;resultMode = MeasureSpec.AT_MOST;}break;// Parent asked to see how big we want to becase MeasureSpec.UNSPECIFIED:if (childDimension >= 0) {// Child wants a specific size... let him have itresultSize = childDimension;resultMode = MeasureSpec.EXACTLY;} else if (childDimension == LayoutParams.MATCH_PARENT) {// Child wants to be our size... find out how big it should// beresultSize = 0;resultMode = MeasureSpec.UNSPECIFIED;} else if (childDimension == LayoutParams.WRAP_CONTENT) {// Child wants to determine its own size.... find out how// big it should beresultSize = 0;resultMode = MeasureSpec.UNSPECIFIED;}break;}return MeasureSpec.makeMeasureSpec(resultSize, resultMode);}

总结

子view的MeasureSpec创建规则如下表所示

 


http://chatgpt.dhexx.cn/article/Vvc7W54B.shtml

相关文章

关于google浏览器打不开网页问题之容易被忽略的点

其实google浏览器打不开 网页,原因网上有好多种,包括什么关闭防火墙、取消高级设置LAN单选框等,我也都试了,搞到最后要崩溃了,后来无意中,我输入一个http://baidu.com然后enter管用了,能打开页面…

谷歌浏览器打不开网页

今天起来发现谷歌浏览器和IE都打不开网页了,估计是我电脑代理又被修改了 在谷歌浏览器的设置--> 高级 --> 打开代理设置中 取消勾选即可修复问题。

关于谷歌浏览器打不开的解决方法

关于谷歌浏览器打不开的解决方法 打开Google,搜索,出现下面的问题,怎么解决呢,下面两种方法提供参考。 打开Google,首页显示输入网址,我们可以输入任意一个网址,例如www.baidu.com,然后就可以搜索了。 打开选项-设置…

谷歌浏览器(chrome)无法正常打开网页的解决办法

在网上看到许多新手想使用谷歌浏览器但是下载安装之后却无法打开网页 分析原因如下: 一般都是因为谷歌浏览器默认的地址栏搜索引擎为goole,由于goole属于国外的网站,我们访问是需要fan qiang才能访问的,所以无法打开网页。所以我…

chrome双击突然打不开的解决办法

这个也是没有想到,浪费了我挺长时间。我电脑的chrome突然打不开了,打不开的意思是双击了之后没有反应,但是其实是有打开进程的,这个就很坑。 网上搜了很多,什么把进程给杀掉的,重启电脑的,重装c…

Outlook 突然打不开

打开电脑正准备上班然后outlook崩了,报错建议我重装软件。。问题是现在用的都是365全家桶,也没办法单独重装一个outlook。盲试了一把repair居然修好了..再后来就经常用到它T_T..(不是什么好事) 首先有几种临时解决方法。 如果时间…

谷歌浏览器任何页面都打不开连设置也不能打开

谷歌浏览器任何页面都打不开 设置不能打开 找到谷歌右击选择属性 点击目标在最后加上 -no-sandbox即可解决 一定记得在 -no-sandbox前加上空格

谷歌浏览器网页打不开怎么办

首次下载谷歌浏览器,打开时在搜索框输入特定的网址会出现下面的界面 相信很多小伙伴也为此苦恼,今天小编为大家分享一个解决方法。 1.在其他其他浏览器搜索【极简插件】如下: 2.在右上角的搜索框里搜索【IGG】 点击推荐下载 3.将下载的压…

Chrome应用商店打不开问题

Chrome浏览器作为谷歌的亲儿子,实力方面很强。可是Chrome自从退出中国市场后一直对国内用户不太友好,但也还是有一批Chrome的忠实粉一直在用Chrome。 Chrome应用商店中有很多好玩实用的插件,我们在国内打开Chrome应用商店时是这样子的&#…

google打不开?更改一下chrome设置,畅通无阻玩谷歌

最近不知道是怎么回事,google(谷歌)一直打不开,gmail也是时断时续,十分痛苦。google服务对于的重要性不言而喻,特别是做 一些国外的项目,google的服务中断会严重影响收益。其实,只要…

sql 抛出异常raiserror()

说明 用于抛出一个异常或错误。这个错误可以被程序捕捉到。 实例 declare error_mes varchar(1000) set error_mes1314520886的ERP_ICStockBillEntry中间表数据的收料仓库编码不存在于系统中 raiserror(error_mes,13,1,张三)输出

SQL 中 RAISERROR 的用法

raiserror 是由单词 raise error 组成 raise 增加; 提高; 提升 raiserror 的作用 : raiserror 是用于抛出一个错误。[ 以下资料来源于sql server 2005的帮助 ] 其语法如下: RAISERROR ( { msg_id | msg_str | local_variable } …

RAISERROR (Transact-SQL)

从msdn上找到的,很详细了生成错误消息并启动会话的错误处理。RAISERROR 可以引用 sys.messages 目录视图中存储的用户定义消息,也可以动态建立消息。该消息作为服务器错误消息返回到调用应用程序,或返回到 TRY…CATCH 构造的关联 CATCH 块。 …

RAISE_APPLICATION_ERROR 用法

可能不是很多人知道 RAISE_APPLICATION_ERROR 的用途是什么,虽然从字面上已经猜到这个函数是干什么用的。平时用来测试的异常处理 我们都是通过dbms_output.put_line来输出异常信息,但是在实际的应用中,需要把异常信息返回给调用的客户端。 …

RAISERROR 的用法(转)

raiserror 的作用: raiserror 是用于抛出一个错误。[ 以下资料来源于sql server 2005的帮助 ] 其语法如下: RAISERROR ( { msg_id | msg_str | local_variable } { ,severity ,state } [ ,argument [ ,...n ] ] ) [ WITH option…

raiserror的用法

描述:raiserror :是用于抛出一个错误 第一个参数:{ msg_id | msg_str | local_variable } msg_id:表示可以是一个sys.messages表中定义的消息代号; 使用 sp_addmessage 存储在 sys.messages 目录视图中的用户定义错误消…

strerror函数

strerror是一个库函数,这个函数的功能就是将errno转换为方便我们理解的字符串信息。我们可以在linux终端命令行中输入“man 3 strerror”来查看这个库函数的详细信息,如下图所示。 从上图我们可以知道,该库函数的原型是:char *st…

SystemError: initialization of _internal failed without raising an exception

运行mmdetection3d时报错: 原因: numba和numpy版本不匹配 解决方法: 降低numpy的版本(由于mmdet3d限制了numba的版本)pip install numpy1.23.1 -i https://pypi.tuna.tsinghua.edu.cn/simple

RAISEERROR

raiserror 的作用 : raiserror 是用于抛出一个错误。[ 以下资料来源于sql server 2005的帮助 ] 其语法如下: RAISERROR ( { msg_id | msg_str | local_variable } { ,severity ,state } [ ,argument [ ,...n…

RAISE_APPLICATION_ERROR用法

 RAISE_APPLICATION_ERROR用法 Posted on 2008-03-30 11:25 CaizhanshusBlog 阅读( 35132) 评论( 0) 编辑 收藏 可能不是很多人知道 RAISE_APPLICATION_ERROR 的用途是什么,虽然从字面上已经猜到这个函数是干什么用的。平时用来测试的异…