MeasureSpec介绍及使用详解

本文深入解析MeasureSpec的概念及其在Android布局中的应用方式。介绍了MeasureSpec的三种模式:UNSPECIFIED、EXACTLY、AT_MOST,并详细解释了MeasureSpec的常用函数及其实现原理。此外,还通过实例展示了如何使用MeasureSpec进行视图测量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一个MeasureSpec封装了父布局传递给子布局的布局要求,每个MeasureSpec代表了一组宽度和高度的要求。一个MeasureSpec有大小和模式组成。他有三种模式:

UNSPECIFIED 未指定 父元素不对字元素施加任何束缚,子元素可以得到任意想要的大小。

EXACTLY 完全 父元素决定自元素的大小,子元素将被限定在给定的边界里而忽略它本身的大小。

AT_MOST 至多 子元素多达到指定大小的值

它常用的三个函数:

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

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

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

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

 

[html]  view plain  copy
 
  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]  view plain  copy
 
  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的值。

转载于:https://www.cnblogs.com/hun2014/p/5898217.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值