理解 MeasureSpec

article/2025/8/25 16:58:54
在开始本篇文章之前,我们先看一段代码:
    @Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int expendSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);super.onMeasure(widthMeasureSpec, expendSpec);}

是不是很熟悉,没错,它就是我们在 ScrollView 嵌套 ListView 的时候,重写 ListView 来处理 ListView 数据显示不全的问题,那么为什么要这么写呢,而 MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST) 又是什么意思呢,别着急,重头戏马上到来。

1. MeasureSpec 是干什么的

确切的说,MeasureSpec 在很大程度上决定了一个 View 的尺寸规格,之所以说很大程度上是因为这个过程还受父容器的影响,因为父容器影响 View 的 MeasureSpec 创建过程,在测量过程中,系统会将 View 的 LayoutParams 根据父容器所施加的规转换成对应的 MeasureSpec,然后再根据这个 MeasureSpec 来测量出 View 的宽高,上面提到过,这里的宽高是测量的宽高,不一定等于 View 的最终宽高,MeasureSpec 看起来有点复杂,其实他的实现还是很简单的,下面会详细的分析MeasureSpec 

2. MeasureSpec 的原理


先来看一张图,带着问题来解读接下来要说的内容。

MeasureSpec 代表一个32为的 int 值,高两位是 SpecMode,低30位是 SpecSize,SpecMode 是指测量模式,而 SpecMode 是指在某种测量模式下的规格大小,下面先看下 MeasureSpec 内部的一些常量的定义,通过下面的代码,应该不难理解 MeasureSpec 的工作原理:

        private static final int MODE_SHIFT = 30;private static final int MODE_MASK  = 0x3 << MODE_SHIFT;/** @hide */@IntDef({UNSPECIFIED, EXACTLY, AT_MOST})@Retention(RetentionPolicy.SOURCE)public @interface MeasureSpecMode {}/*** Measure specification mode: The parent has not imposed any constraint* on the child. It can be whatever size it wants.*/public static final int UNSPECIFIED = 0 << MODE_SHIFT;/*** Measure specification mode: The parent has determined an exact size* for the child. The child is going to be given those bounds regardless* of how big it wants to be.*/public static final int EXACTLY     = 1 << MODE_SHIFT;/*** Measure specification mode: The child can be as large as it wants up* to the specified size.*/public static final int AT_MOST     = 2 << MODE_SHIFT;/*** Creates a measure specification based on the supplied size and mode.** The mode must always be one of the following:* <ul>*  <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>*  <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>*  <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>* </ul>** <p><strong>Note:</strong> On API level 17 and lower, makeMeasureSpec's* implementation was such that the order of arguments did not matter* and overflow in either value could impact the resulting MeasureSpec.* {@link android.widget.RelativeLayout} was affected by this bug.* Apps targeting API levels greater than 17 will get the fixed, more strict* behavior.</p>** @param size the size of the measure specification* @param mode the mode of the measure specification* @return the measure specification based on size and mode*/public static int makeMeasureSpec(@IntRange(from = 0, to = (1 << MeasureSpec.MODE_SHIFT) - 1) int size,@MeasureSpecMode int mode) {if (sUseBrokenMakeMeasureSpec) {return size + mode;} else {return (size & ~MODE_MASK) | (mode & MODE_MASK);}}/*** Like {@link #makeMeasureSpec(int, int)}, but any spec with a mode of UNSPECIFIED* will automatically get a size of 0. Older apps expect this.** @hide internal use only for compatibility with system widgets and older apps*/public static int makeSafeMeasureSpec(int size, int mode) {if (sUseZeroUnspecifiedMeasureSpec && mode == UNSPECIFIED) {return 0;}return makeMeasureSpec(size, mode);}/*** Extracts the mode from the supplied measure specification.** @param measureSpec the measure specification to extract the mode from* @return {@link android.view.View.MeasureSpec#UNSPECIFIED},*         {@link android.view.View.MeasureSpec#AT_MOST} or*         {@link android.view.View.MeasureSpec#EXACTLY}*/@MeasureSpecModepublic static int getMode(int measureSpec) {//noinspection ResourceTypereturn (measureSpec & MODE_MASK);}/*** Extracts the size from the supplied measure specification.** @param measureSpec the measure specification to extract the size from* @return the size in pixels defined in the supplied measure specification*/public static int getSize(int measureSpec) {return (measureSpec & ~MODE_MASK);}

MeasureSpec 通过将 SpecMode 和 SpecSize 打包成一个 int 值来避免过多的对象内存分配,为了方便操作,其提供了打包和解包的方法。SpecMode 和 SpecSize 也是一个 int 值,一组 SpecMode 和 SpecSize 可以打包成一个 MeasureSpec,而一个 MeasureSpec 可以通过解包的方法来得出原始的 SpecMode 和 SpecSize,需要注意的是这里提到的 MeasureSpec 是值 MeasureSpec 所代表的 int 值,而并非 MeasureSpec 本身。

3. MeasureSpec 的三种模式

(1)UNSPECIFIEND

父容易不对 View 有任何影响,要多大给多大,这种情况一般用于系统内部,表示一种测量模式(我也不是太懂,有懂的大佬指点下)

(2)EXACTLY

父容器已经检测出 View 所需要的精确大小,这个时候 View 的最终大小就是 MeasureSpec 所指定的值,它对应于 LayoutParams 的 match_parent 和具体的数值两种模式。(如果是重写控件,慎用该模式,不然你会发现一件很神奇的事情,想知道的可以自己动手试试)

(3)AT_MOST

父容器指定一个大小即 SpecSize,View 的大小不能大于这个值,具体是什么值要看不同 View 的具体实现,它对应于 LayoutParams 的warp_content。

4.MeasureSpec 和 LayoutParams 对应关系

上面提到,系统内部是通过 MeasureSpec 来进行 View 的测量的,但是正常情况下我们使用 View 指定 MeasureSpec,尽管如此,但是我们给 View 设置 LayoutParams,在 View 测量的时候,系统会将 LayoutParams 在父容器的约束下转换成 MeasureSpec,然后在根据  MeasureSpec 来确定 View 测量后的宽高,需要注意的是 MeasureSpec 不是唯一有 LayoutParams 决定的,LayoutParams 需要和父容器一起才能决定 View 的 MeasureSpec,从而进一步决定 View 的宽高。另外,对于顶级 View(DecorView)和普通 View 来说,MeasureSpec 的转换过程略有不同,其 MeasureSpec 有窗口的尺寸和其自身的 LayoutParams 来共同决定;对于普通的 View,其 MeasureSpec 由父容器的 MeasureSpec 和 自身的 LayoutParams 决定,MeasureSpec 一旦确定后,onMeasure 中就可以确定 View 的测量宽高。
对于 DecorView 来说,在RootViewImpl 中的 measureHierarcly 方法中 有如下一段代码,它展示了 DecorView 的 MeasureSpec 的创建过程,其中 desiredWindowWidth 和 desiredWindowHeight 是屏幕的尺寸大小:
        childWidthMeasureSpec = getRootMeasureSpec(desiredWindowWidth, lp.width);childHeightMeasureSpec = getRootMeasureSpec(desiredWindowHeight, lp.height);performMeasure(childWidthMeasureSpec, childHeightMeasureSpec);
接着再看一下 getRootMeasureSpec 方法实现:
/*** Figures out the measure spec for the root view in a window based on it's* layout params.** @param windowSize*            The available width or height of the window** @param rootDimension*            The layout params for one dimension (width or height) of the*            window.** @return The measure spec to use to measure the root view.*/private static int getRootMeasureSpec(int windowSize, int rootDimension) {int measureSpec;switch (rootDimension) {case ViewGroup.LayoutParams.MATCH_PARENT:// Window can't resize. Force root view to be windowSize.measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.EXACTLY);break;case ViewGroup.LayoutParams.WRAP_CONTENT:// Window can resize. Set max size for root view.measureSpec = MeasureSpec.makeMeasureSpec(windowSize, MeasureSpec.AT_MOST);break;default:// Window wants to be an exact size. Force root view to be that size.measureSpec = MeasureSpec.makeMeasureSpec(rootDimension, MeasureSpec.EXACTLY);break;}return measureSpec;}
根据上述代码, DecorView 的 MeasureSpec 的产生过程就很明了了,具体来说遵守如下规则,根据它的 LayoutParams 中的宽高的参数来划分:
(1)LayoutParams.MATCH_PARENT:精确模式,大小就是窗口大小;
(2)LayoutParams.WARP_CONTENT:最大模式,大小不定,但是不能超过窗口的大小
(3)固定大小(不如100DP):精确模式,大小为 LayoutParams 中指定的大小。
对于 View 来说,这里是指我们布局中的 View,View 的 measure 过程由 ViewGroup 传递过来,先看一下 ViewGroup 的 measureChildWithMargins2 方法:
    // Note: padding has already been removed from the supplied specsprivate void measureChildWithMargins2(View child, int parentWidthSpec, int parentHeightSpec,int childWidth, int childHeight) {int childWidthSpec = getChildMeasureSpec(parentWidthSpec,getTotalMargin(child, true), childWidth);int childHeightSpec = getChildMeasureSpec(parentHeightSpec,getTotalMargin(child, false), childHeight);child.measure(childWidthSpec, childHeightSpec);}
上述方法会对子元素进行 measure,在调用子元素的 measure 方法之前会先调通过 getChildMeasureSpec 的创建与父容器的 MeasureSpec 和子元素本身的 LayoutParams 有关,此外还和 View 的 margins 及 padding 有关,具体情况可以看一下 ViewGroup 的 getChildMeasureSpec 方法,如下所示:
   /*** 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 = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;resultMode = MeasureSpec.UNSPECIFIED;} else if (childDimension == LayoutParams.WRAP_CONTENT) {// Child wants to determine its own size.... find out how// big it should beresultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;resultMode = MeasureSpec.UNSPECIFIED;}break;}//noinspection ResourceTypereturn MeasureSpec.makeMeasureSpec(resultSize, resultMode);}
上述方法不难理解,它的主要作用是根据父容器的 MeasureSpec 同时结合 View 本身的 LayoutParams 来确定子元素的 MeasureSpec ,参数中的 padding 是指父容器中已经占用的空间大小,因此子元素可用的大小为父容器的尺寸减去 padding ,具体代码如下:
    int specSize = MeasureSpec.getSize(spec);int size = Math.max(0, specSize - padding);
getChildMeasureSpec 清楚展示了普通 View 的 MeasureSpec 的创建规则。这个整个过程正好对应了文章开头那个表格,然后咱们再回来看下这个表格:
针对上图,这里再做下说明,前面已经提到,对于普通的 View,其 MeasureSpec 有父容器的 MeasureSpec 和自身的 LayoutParams 来决定的,那么针对不同的父容器和 View 本身不同的 LayoutParams,View 就可以有多张 MeasureSpec。这里简单说一下,当 View 采用固定宽高的时候,不管父容器的 MeasureSpec 是什么,View 的 MeasureSpec 都是精确模式并且大小遵循 LayoutParams 中的大小,当 View 中的宽高是 match_parent 时,如果父容器的模式是精准模式,那么 View 也是精准模式,其大小是父容器的剩余空间,如果父容器是最大模式,那么 View 也是最大模式但大小不会超过父容器的剩余空间。当 View 的宽高是 warp_content 时,不管父容器的模式是精准还是最大模式, View 的模式总是最大模式但大小不会超过父容器的剩余空间。至于 UNSPECIFIEND 模式,我是真的不知道有啥用,等以后弄明白了再补充吧,或者哪位大神知道,可以说下,谢谢!!
通过上边的表格可以看出,我们知道了父容器的 MeasureSpec 和子元素的 LayoutParams,就可以快速地确定出子元素的 MeasureSpec 了,有了 MeasureSpec 就可以进一步确定出子元素测量后的大小了。



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

相关文章

对MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE 2, MeasureSpec.AT_MOST)的一点理解

之前 遇到ScrollView中嵌入ListView&#xff0c;GridView冲突的解决&#xff08;让ListView全显示出来&#xff09; 链接 网上查找资料&#xff0c;代码大致如下&#xff1a; import android.content.Context; import android.util.AttributeSet; import android.widget.ListV…

View的基本概念与MeasureSpec

1.基本概念 View的绘制是由measuer、layout、draw三个过程才能完整的绘制一个View&#xff0c;其中measure是测量View的宽、高&#xff0c;layout是为了确认View在父容器所在的位置&#xff0c;draw是负责在屏幕上将View绘制出来。View的绘制流程是从ViewRoot的performTraversa…

Android之:了解MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE 2,MeasureSpec.AT_MOST)

在自定义View和ViewGroup的时候&#xff0c;我们经常会遇到int型的 MeasureSpec 来表示一个组件的大小&#xff0c;这个变量里面不仅有组件的尺寸大小&#xff0c;还有大小的模式。 这个大小的模式&#xff0c;有点难以理解。在系统中组件的大小模式有三种&#xff1a; 1.精确…

Android开发 MeasureSpec介绍

搬家后的博客链接: IT客栈 www.itkezhan.org 在自定义View和ViewGroup的时候&#xff0c;我们经常会遇到int型的MeasureSpec来表示一个组件的大小&#xff0c;这个变量里面不仅有组件的尺寸大小&#xff0c;还有大小的模式。 这个大小的模式&#xff0c;有点难以理解。在系统中…

Android-测量规格(MeasureSpec)

目录 一、简介二、组成三、具体使用 一、简介 二、组成 测量规格(MeasureSpec)是由测量模式(mode)和测量大小(size)组成&#xff0c;共32位(int类型)&#xff0c;其中&#xff1a; 测量模式(mode)&#xff1a;占测量规格(MeasureSpec)的高2位&#xff1b;测量大小(size)&…

MeasureSpec学习 - 转

在自定义View和ViewGroup的时候&#xff0c;我们经常会遇到int型的 MeasureSpec 来表示一个组件的大小&#xff0c;这个变量里面不仅有组件的尺寸大小&#xff0c;还有大小的模式。 这个大小的模式&#xff0c;有点难以理解。在系统中组件的大小模式有三种&#xff1a; 1.精确…

MeasureSpec介绍

在自定义View和ViewGroup的时候&#xff0c;我们经常会遇到int型的MeasureSpec来表示一个组件的大小&#xff0c;这个变量里面不仅有组件的尺寸大小&#xff0c;还有大小的模式。 这个大小的模式&#xff0c;有点难以理解。在系统中组件的大小模式有三种&#xff1a; 1.精确模式…

Android 中MeasureSpec的创建规则

概述 在Android中&#xff0c;View的onMeasure()方法用来对控件进行测量&#xff0c;确定控件的宽高。该方法的两个参数widthMeasureSpec和heightMeasureSpec由父View计算后传入子view的measure()方法&#xff0c;再由子view的measure()方法传入onMeasure()方法&#xff0c;本…

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

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

谷歌浏览器打不开网页

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

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

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

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

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

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

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

Outlook 突然打不开

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

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

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

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

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

Chrome应用商店打不开问题

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

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

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

sql 抛出异常raiserror()

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

SQL 中 RAISERROR 的用法

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