MeasureSpec介绍及使用详解
来源:互联网 发布:滴滴大数据研究中心 编辑:程序博客网 时间:2024/06/09 19:57
它常用的三个函数:
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)
public static int resolveSize(int size, int measureSpec) { int result = size; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); switch (specMode) { case MeasureSpec.UNSPECIFIED: result = size; break; case MeasureSpec.AT_MOST: result = Math.min(size, specSize); break; case MeasureSpec.EXACTLY: result = specSize; break; } return result; }
上面既然要用到measureSpec值,那自然表示这个函数通常是在onMeasure方法里面调用的。简单说一下,这个方法的主要作用就是根据你提供的大小和模式,返回你想要的大小值,这个里面根据传入模式的不同来做相应的处理。
再看看MeasureSpec.makeMeasureSpec方法,实际上这个方法很简单:
public static int makeMeasureSpec(int size, int mode) { return size + mode; }
这样大家不难理解size跟measureSpec区别了。看看它的使用吧,ListView.measureItem(View child)
private void measureItem(View child) { ViewGroup.LayoutParams p = child.getLayoutParams(); if (p == null) { p = new ViewGroup.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec, mListPadding.left + mListPadding.right, p.width); int lpHeight = p.height; int childHeightSpec; if (lpHeight > 0) { childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY); } else { childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); } child.measure(childWidthSpec, childHeightSpec); }
measureSpec方法通常在ViewGroup中用到,它可以根据模式(MeasureSpec里面的三个)可以调节子元素的大小。
注意,使用EXACTLY和AT_MOST通常是一样的效果,如果你要区别他们,那么你就要使用上面的函数View.resolveSize(int size,int measureSpec)返回一个size值,然后使用你的view调用setMeasuredDimension(int,int)函数。
protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) { mMeasuredWidth = measuredWidth; mMeasuredHeight = measuredHeight; mPrivateFlags |= MEASURED_DIMENSION_SET; }
然后你调用view.getMeasuredWidth,view.getMeasuredHeigth 返回的就是上面函数里的mMeasuredWidth,mMeasuredHeight的值。
- MeasureSpec介绍及使用详解
- MeasureSpec介绍及使用详解
- MeasureSpec介绍及使用详解
- MeasureSpec介绍及使用详解
- MeasureSpec介绍及使用详解
- MeasureSpec介绍及使用详解
- MeasureSpec介绍及使用详解
- MeasureSpec介绍及使用详解
- MeasureSpec介绍及使用详解
- MeasureSpec介绍及使用详解
- MeasureSpec介绍及使用详解
- MeasureSpec介绍及使用详解
- MeasureSpec介绍及使用详解
- MeasureSpec介绍及使用详解
- MeasureSpec介绍及使用详解
- MeasureSpec介绍及使用详解
- MeasureSpec介绍及使用详解
- MeasureSpec介绍及使用详解
- Altium Designer 10 导出文件(PDF,gerber,BOM)
- Android4.0 socket编程
- 如何使用Dynamics CRM 2013服务模块03(服务和服务活动)
- 接口继承与实现继承
- ASIHTTPRequest-其他特性
- MeasureSpec介绍及使用详解
- 反编译
- 在外部类以外创建静态内部类的实例 简单实现
- [MySQL] 获取元数据的方法
- [置顶] 你为什么得不到晋升
- nginx 301重定向
- ASIHTTPRequest-Debug选项
- win7 64bit redis安装
- strtok和wcstok分割字符串函数