`

MeasureSpec介绍及使用详解

 
阅读更多

一个MeasureSpec封装了父布局传递给子布局的布局要求,每个MeasureSpec代表了一组宽度和高度的要求。一个MeasureSpec由大小和模式组成。它有三种模式:UNSPECIFIED(未指定),父元素部队自元素施加任何束缚,子元素可以得到任意想要的大小;EXACTLY(完全),父元素决定自元素的确切大小,子元素将被限定在给定的边界里而忽略它本身大小;AT_MOST(至多),子元素至多达到指定大小的值。

 

  它常用的三个函数:

  1.static int getMode(int measureSpec):根据提供的测量值(格式)提取模式(上述三个模式之一)

  2.static int getSize(int measureSpec):根据提供的测量值(格式)提取大小值(这个大小也就是我们通常所说的大小)

  3.static int makeMeasureSpec(int size,int mode):根据提供的大小值和模式创建一个测量值(格式)

 

  这个类的使用呢,通常在view组件的onMeasure方法里面调用但也有少数例外,看看几个例子:

 

  a.首先一个我们常用到的一个有用的函数,View.resolveSize(int size,int measureSpec)

 

复制代码
8421     public static int resolveSize(int size, int measureSpec) {
8422         int result = size;
8423         int specMode = MeasureSpec.getMode(measureSpec);
8424         int specSize =  MeasureSpec.getSize(measureSpec);
8425         switch (specMode) {
8426         case MeasureSpec.UNSPECIFIED:
8427             result = size;
8428             break;
8429         case MeasureSpec.AT_MOST:
8430             result = Math.min(size, specSize);
8431             break;
8432         case MeasureSpec.EXACTLY:
8433             result = specSize;
8434             break;
8435         }
8436         return result;
8437     }
复制代码

  上面既然要用到measureSpec值,那自然表示这个函数通常是在onMeasure方法里面调用的。简单说一下,这个方法的主要作用就是根据你提供的大小和模式,返回你想要的大小值,这个里面根据传入模式的不同来做相应的处理。

  再看看MeasureSpec.makeMeasureSpec方法,实际上这个方法很简单:

9023         public static int makeMeasureSpec(int size, int mode) {
9024             return size + mode;
9025         }

  这样大家不难理解size跟measureSpec区别了。看看它的使用吧,ListView.measureItem(View child)

 

复制代码
2464     private void measureItem(View child) {
2465         ViewGroup.LayoutParams p = child.getLayoutParams();
2466         if (p == null) {
2467             p = new ViewGroup.LayoutParams(
2468                     ViewGroup.LayoutParams.MATCH_PARENT,
2469                     ViewGroup.LayoutParams.WRAP_CONTENT);
2470         }
2471 
2472         int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec,
2473                 mListPadding.left + mListPadding.right, p.width);
2474         int lpHeight = p.height;
2475         int childHeightSpec;
2476         if (lpHeight > 0) {
2477             childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);
2478         } else {
2479             childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
2480         }
2481         child.measure(childWidthSpec, childHeightSpec);
2482     }
复制代码

  measureSpec方法通常在ViewGroup中用到,它可以根据模式(MeasureSpec里面的三个)可以调节子元素的大小。

  注意,使用EXACTLY和AT_MOST通常是一样的效果,如果你要区别他们,那么你就要使用上面的函数View.resolveSize(int size,int measureSpec)返回一个size值,然后使用你的view调用setMeasuredDimension(int,int)函数。

 

8406     protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
8407         mMeasuredWidth = measuredWidth;
8408         mMeasuredHeight = measuredHeight;
8409 
8410         mPrivateFlags |= MEASURED_DIMENSION_SET;
8411     }

  然后你调用view.getMeasuredWidth,view.getMeasuredHeigth 返回的就是上面函数里的mMeasuredWidth,mMeasuredHeight的值。

分享到:
评论

相关推荐

    MeasureSpec三种测量模式详解例子

    MeasureSpec三种测量模式详解例子

    Android MeasureSpec的理解和源码的解析

    主要介绍了Android MeasureSpec的理解和源码的解析的相关资料,希望通过本文大家可以掌握理解这部分内容,需要的朋友可以参考下

    类似时光网IOS城市列表(ListView)效果.zip

    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } 搜索功能其实没写进demo里面,从项目里整理出来太麻烦了。...

    Android自定义View

    介绍如何自定义View,并重写onMeasure方法重新测量高度和宽度, 讲解MeasureSpec类的使用方法 详见博文 http://blog.csdn.net/a87b01c14/article/details/50359226

    android自定义控件-侧滑菜单

    mode共有三种情况,取值分别为MeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY, MeasureSpec.AT_MOST。 MeasureSpec.EXACTLY是精确尺寸, 当我们将控件的layout_width或layout_height指定为具体数值时如andorid ...

    Android开发艺术探索.任玉刚(带详细书签).pdf

    第一,介绍Android开发者不容易掌握的一些知识点;第二,结合Android源代码和应用层开发过程,融会贯通,介绍一些比较深入的知识点;第三,介绍一些核心技术和Android的性能优化思想。 第1章 Activity的生命周期和...

    Android开发艺术探索

    第一,介绍Android开发者不容易掌握的一些知识点;第二,结合Android源代码和应用层开发过程,融会贯通,介绍一些比较深入的知识点;第三,介绍一些核心技术和Android的性能优化思想。 《Android开发艺术探索》侧重...

    Android OnCreate()中获取控件高度与宽度两种方法详解

    Android OnCreate()中获取控件高度与宽度 ... int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); ssidtext.m

    android 实现FlowLayout 流线布局(自定义ViewGroup)

    2、将MeasureSpec、View的绘制流程、Layoutparams等分散的知识点整合成一个demo。 #项目灵感 笔者经验也是有限,此文章主要借鉴张鸿洋前辈的博客。 原文地址:...

    仿微信朋友圈图片查看

    import android.content.Context;... int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }

    Android视图控件架构分析之View、ViewGroup

    在Android中,视图控件大致被分为两类,即ViewGroup和View,ViewGroup控件作为父控件,包含并管理着子View,通过...1.MeasureSpec包含了测量的模式和测量的大小,通过MeasureSpec.getMode()获取测量模式,通过MeasureSp

    Android实现Z轴布局效果

    如果需要在布局中创造...layoutParams的作用是向父布局请求布局参数(MeasureSpec),这个参数会在View inflate时添加到布局中,我们如果使用LayoutParams将会得到很大的方便 // 这里继承FrameLayout的LayoutParams即可 p

    在ScorllView中ListView和GridView冲突解决方案

    在ScorllView中ListView和GridView冲突解决方案 在自定义View和ViewGroup的时候,我们经常会遇到int型的MeasureSpec来表示一个组件的大小。

    类似时光网IOS城市列表(ListView)效果

    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } 搜索功能其实没写进demo里面,从项目里整理出来太麻烦了...

    android异步生成图片的示例代码

    下面来说说在Android上如果异步生成图片,通过xml布局用View排版好图片样式,在子线程生成一张图片,以满足生成用来分享的... int widthSpec = View.MeasureSpec.makeMeasureSpec(view.getLayoutParams().width, Vie

    解析:继承ViewGroup后的子类如何重写onMeasure方法

    1.首先贴上我试验成功的代码 代码如下:protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int measureWidth = MeasureSpec.getSize(widthMeasureSpec); int measureHeigth = MeasureSpec....

    浅析Android中getWidth()和getMeasuredWidth()的区别

    结论:getMeasuredWidth()获取的是view...这个值是一个8位的十六进制的数字,高两位表示的是这个measure阶段的Mode的值,具体可以查看MeasureSpec的原理。这里mMeasuredWidth & MEASURED_SIZE_MASK表示的是测量阶段结

    Android开发之自定义CheckBox

    要实现的效果如下 考虑到关键是动画效果,所以直接继承View。不过CheckBox的超类CompoundButton实现了... * A MeasureSpec encapsulates the layout requirements passed from parent to child. * Each MeasureSp

Global site tag (gtag.js) - Google Analytics