博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MeasureSpec介绍及使用详解
阅读量:3637 次
发布时间:2019-05-21

本文共 2206 字,大约阅读时间需要 7 分钟。

一个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方法里面调用.

看看它的使用吧,ListView.measureItem(View child)

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

[html]
  1. public static int resolveSize(int size, int measureSpec) {  
  2.          int result = size;  
  3.          int specMode = MeasureSpec.getMode(measureSpec);  
  4.          int specSize =  MeasureSpec.getSize(measureSpec);  
  5.          switch (specMode) {  
  6.          case MeasureSpec.UNSPECIFIED:  
  7.              result = size;  
  8.              break;  
  9.          case MeasureSpec.AT_MOST:  
  10.              result = Math.min(size, specSize);  
  11.              break;  
  12.          case MeasureSpec.EXACTLY:  
  13.              result = specSize;  
  14.              break;  
  15.          }  
  16.          return result;  
  17.      }  
9023         public static int makeMeasureSpec(int size, int mode) {9024             return size + mode;9025         }

[html]
  1. private void measureItem(View child) {  
  2.          ViewGroup.LayoutParams p = child.getLayoutParams();  
  3.          if (p == null) {  
  4.              p = new ViewGroup.LayoutParams(  
  5.                      ViewGroup.LayoutParams.MATCH_PARENT,  
  6.                      ViewGroup.LayoutParams.WRAP_CONTENT);  
  7.          }  
  8.    
  9.          int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec,  
  10.                  mListPadding.left + mListPadding.right, p.width);  
  11.          int lpHeight = p.height;  
  12.          int childHeightSpec;  
  13.          if (lpHeight > 0) {  
  14.              childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);  
  15.          } else {  
  16.              childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);  
  17.          }  
  18.          child.measure(childWidthSpec, childHeightSpec);  
  19.      }  

 注意,使用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的值。

转载地址:http://wcrun.baihongyu.com/

你可能感兴趣的文章
IDEA-忽略文件显示
查看>>
UnsupportedClassVersionError-异常解决
查看>>
Mysql (InnoDB&MyISAM )-如何在两种存储引擎中进行选择?
查看>>
SpringAop两种代理模式-源码分析
查看>>
IDEA-自定义常用代码块
查看>>
JAVA多线程-JUC-8锁
查看>>
Vue-实现对象拷贝
查看>>
export 命令导出变量
查看>>
JAVA-快速接入第三方应用登录(QQ、微信、微博)
查看>>
解决Mysql-无法批量更新的问题
查看>>
Springboot-logback配色方案
查看>>
面试题-给定一个“flatten”Dictionary对象,根据键转换成嵌套字典对象
查看>>
用cookies实现主题背景颜色切换,保存选择的颜色
查看>>
用 node.js 开启一个 http服务,返回文件或信息
查看>>
【git】warning: adding embedded git repository
查看>>
git warning: LF will be replaced by CRLF in 解决办法
查看>>
python文件处理
查看>>
CentOS7制作本地yum源
查看>>
参考花书《深度学习》实现一个简易版PCA
查看>>
CSDN Markdown编辑器——文本颜色、大小、字体设计
查看>>