View的onMeasure方法

本文深入解析了Android中View的测量机制,包括MeasureSpec的作用及其三种模式(UNSPECIFIED, EXACTLY, AT_MOST),并详细解释了fill_parent与wrap_content在不同模式下对View尺寸的影响。同时提供了onMeasure方法的重写示例。

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

http://blog.youkuaiyun.com/czh0766/article/details/5846460

View在屏幕上显示出来要先经过measure和layout. 在调用onMeasure(int widthSpec, int heightSpec)方法时,要涉及到MeasureSpec的使用,MeasureSpec有3种模式分别是UNSPECIFIED, EXACTLY和AT_MOST, 那么这些模式和我们平时设置的layout参数fill_parent, wrap_content有什么关系呢。经过代码测试就知道,当我们设置width或height为fill_parent时,容器在布局时调用子view的measure方法传入的模式是EXACTLY,因为子view会占据剩余容器的空间,所以它大小是确定的。而当设置为wrap_content时,容器传进去的是AT_MOST, 表示子view的大小最多是多少,这样子view会根据这个上限来设置自己的尺寸。当子view的大小设置为精确值时,容器传入的是EXACTLY, 而MeasureSpec的UNSPECIFIED模式目前还没有发现在什么情况下使用。

   View的onMeasure方法默认行为是当模式为UNSPECIFIED时,设置尺寸为mMinWidth(通常为0)或者背景drawable的最小尺寸,当模式为EXACTLY或者AT_MOST时,尺寸设置为传入的MeasureSpec的大小。

   有个观念需要纠正的是,fill_parent应该是子view会占据剩下容器的空间,而不会覆盖前面已布局好的其他view空间,当然后面布局子view就没有空间给分配了,所以fill_parent属性对布局顺序很重要。以前所想的是把所有容器的空间都占满了,难怪google在2.2版本里把fill_parent的名字改为match_parent.

http://blog.youkuaiyun.com/wud_jiyanhui/article/details/5828504

关于onMeasure

1、onMeasure方法在控件的父元素正要放置它的子控件时调用。它会问一个问题,“你想要用多大地方啊?”,然后传入两个参数——widthMeasureSpec 和heightMeasureSpec。它们指明控件可获得的空间以及关于这个空间描述的元数据。

2、默认的onMeasure提供的大小是 100 * 100 所以你想设置自己view的大小,需要重写onMeasureonDraw方法

3、如何重写onMeasure。注意,调用的本地空方法是来计算高度和宽度的。它们会译解 widthHeightSpec和heightMeasureSpec值,并计算出合适的高度和宽度值。

比返回一个结果要好的方法是你传递View的高度和宽度到setMeasuredDimension方法里。  
接下来的代码片段给出了如何重写onMeasure。注意,调用的本地空方法是来计算高度和宽度的。它们会译解widthHeightSpec和heightMeasureSpec值,并计算出合适的高度和宽度值。


@Override

protected void onMeasure(int wMeasureSpec, int hMeasureSpec) {

int measuredHeight = measureHeight(hMeasureSpec);

int measuredWidth = measureWidth(wMeasureSpec);

// 必须调用setMeasuredDimension方法

// 否则当控件放置时会引发一个运行时异常。

setMeasuredDimension(measuredHeight, measuredWidth);

}
private int measureHeight(int measureSpec) {

int specMode = MeasureSpec.getMode(measureSpec);

int specSize = MeasureSpec.getSize(measureSpec);

[ ... 计算View的高度 ... ]

return specSize;

}

private int measureWidth(int measureSpec) {

int specMode = MeasureSpec.getMode(measureSpec);

int specSize = MeasureSpec.getSize(measureSpec);

[ ... 计算View的宽度 ... ]

return specSize;

}

边界参数——widthMeasureSpec和heightMeasureSpec ,效率的原因以整数的方式传入。在它们使用之前,首先要做的是使用MeasureSpec类的静态方法getModegetSize来译解,如下面的片段所示:

int specMode = MeasureSpec.getMode(measureSpec);

int specSize = MeasureSpec.getSize(measureSpec);

依据specMode 的值:

如果是AT_MOST,specSize 代表的是最大可获得的空间;

如果是EXACTLY,specSize 代表的是精确的尺寸;

如果是UNSPECIFIED,对于控件尺寸来说,没有任何参考意义。

当以EXACT方式标记测量尺寸,父元素会坚持在一个指定的精确尺寸区域放置View。在父元素问子元素要多大空间时,AT_MOST指示者会说给我最大的范围。在很多情况下,你得到的值都是相同的。

在两种情况下,你必须绝对的处理这些限制。在一些情况下,它可能会返回超出这些限制的尺寸,在这种情况下,你可以让父元素选择如何对待超出的View,使用裁剪还是滚动等技术。

 

  1. 接下来的框架代码给出了处理View测量的典型实现:  
  2.   
  3.    
  4.   
  5. @Override  
  6.   
  7. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  8.   
  9. int measuredHeight = measureHeight(heightMeasureSpec);  
  10.   
  11. int measuredWidth = measureWidth(widthMeasureSpec);  
  12.   
  13. setMeasuredDimension(measuredHeight, measuredWidth);  
  14.   
  15. }  
  16.   
  17.    
  18.   
  19. private int measureHeight(int measureSpec) {  
  20.   
  21. int specMode = MeasureSpec.getMode(measureSpec);  
  22.   
  23. int specSize = MeasureSpec.getSize(measureSpec);  
  24.   
  25.    
  26.   
  27. // Default size if no limits are specified.  
  28.   
  29. int result = 500;  
  30.   
  31. if (specMode == MeasureSpec.AT_MOST)   
  32.   
  33. {  
  34.   
  35. // Calculate the ideal size of your  
  36.   
  37. // control within this maximum size.  
  38.   
  39. // If your control fills the available  
  40.   
  41. // space return the outer bound.  
  42.   
  43. result = specSize;  
  44.   
  45. }   
  46.   
  47. else if (specMode == MeasureSpec.EXACTLY)   
  48.   
  49. {  
  50.   
  51. // If your control can fit within these bounds return that value.  
  52.   
  53. result = specSize;  
  54.   
  55. }  
  56.   
  57. return result;  
  58.   
  59. }  
  60.   
  61.    
  62.   
  63. private int measureWidth(int measureSpec) {  
  64.   
  65. int specMode = MeasureSpec.getMode(measureSpec);  
  66.   
  67. int specSize = MeasureSpec.getSize(measureSpec);  
  68.   
  69.    
  70.   
  71. // Default size if no limits are specified.  
  72.   
  73. int result = 500;  
  74.   
  75. if (specMode == MeasureSpec.AT_MOST)  
  76.   
  77. {  
  78.   
  79. // Calculate the ideal size of your control  
  80.   
  81. // within this maximum size.  
  82.   
  83. // If your control fills the available space  
  84.   
  85. // return the outer bound.  
  86.   
  87. result = specSize;  
  88.   
  89. }   
  90.   
  91. else if (specMode == MeasureSpec.EXACTLY)   
  92.   
  93. {  
  94.   
  95. // If your control can fit within these bounds return that value.  
  96.   
  97. result = specSize;  
  98.   
  99. }  
  100.   
  101. return result;  
  102.   
  103. }  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值