转自:http://blog.sina.com.cn/s/blog_61fbf8d10100zzoy.html

Android View.onMeasure方法的理解

View在屏幕上显示出来要先经过measure(计算)和layout(布局).
1、什么时候调用onMeasure方法? 
当控件的父元素正要放置该控件时调用.父元素会问子控件一个问题,“你想要用多大地方啊?”,然后传入两个参数——widthMeasureSpec和heightMeasureSpec.
这两个参数指明控件可获得的空间以及关于这个空间描述的元数据.
更好的方法是你传递View的高度和宽度到setMeasuredDimension方法里,这样可以直接告诉父控件,需要多大地方放置子控件.

  接下来的代码片段给出了如何重写onMeasure.注意,调用的本地空方法是来计算高度和宽度的.它们会译解widthHeightSpec和heightMeasureSpec值,并计算出合适的高度和宽度值.
java代码:

  1. @Override
  2. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  3. int measuredHeight = measureHeight(heightMeasureSpec);
  4. int measuredWidth = measureWidth(widthMeasureSpec);
  5. setMeasuredDimension(measuredHeight, measuredWidth);
  6. }
  7. private int measureHeight(int measureSpec) {
  8. // Return measured widget height.
  9. }
  10. private int measureWidth(int measureSpec) {
  11. // Return measured widget width.
  12. }
  13. 边界参数——widthMeasureSpec和heightMeasureSpec ,效率的原因以整数的方式传入。在它们使用之前,首先要做的是使用MeasureSpec类的静态方法getMode和getSize来译解,如下面的片段所示:

    java代码:

    1. int specMode = MeasureSpec.getMode(measureSpec);
    2. int specSize = MeasureSpec.getSize(measureSpec);

    依据specMode的值,(MeasureSpec有3种模式分别是UNSPECIFIED, EXACTLY和AT_MOST)

  14. 如果是AT_MOST,specSize 代表的是最大可获得的空间; 
    如果是EXACTLY,specSize 代表的是精确的尺寸; 
    如果是UNSPECIFIED,对于控件尺寸来说,没有任何参考意义。
    2、那么这些模式和我们平时设置的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.

      在两种情况下,你必须绝对的处理这些限制。在一些情况下,它可能会返回超出这些限制的尺寸,在这种情况下,你可以让父元素选择如何对待超出的View,使用裁剪还是滚动等技术。
      接下来的框架代码给出了处理View测量的典型实现:

    java代码:

    1. @Override
    2. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    3. int measuredHeight = measureHeight(heightMeasureSpec);
    4. int measuredWidth = measureWidth(widthMeasureSpec);
    5. setMeasuredDimension(measuredHeight, measuredWidth);
    6. }
    7. private int measureHeight(int measureSpec) {
    8. int specMode = MeasureSpec.getMode(measureSpec);
    9. int specSize = MeasureSpec.getSize(measureSpec);
    10. // Default size if no limits are specified.
    11. int result = 500;
    12. if (specMode == MeasureSpec.AT_MOST){
    13. // Calculate the ideal size of your
    14. // control within this maximum size.
    15. // If your control fills the available
    16. // space return the outer bound.
    17. result = specSize;
    18. }
    19. else if (specMode == MeasureSpec.EXACTLY){
    20. // If your control can fit within these bounds return that value.
    21. result = specSize;
    22. }
    23. return result;
    24. }
    25. private int measureWidth(int measureSpec) {
    26. int specMode = MeasureSpec.getMode(measureSpec);
    27. int specSize = MeasureSpec.getSize(measureSpec);
    28. // Default size if no limits are specified.
    29. int result = 500;
    30. if (specMode == MeasureSpec.AT_MOST){
    31. // Calculate the ideal size of your control
    32. // within this maximum size.
    33. // If your control fills the available space
    34. // return the outer bound.
    35. result = specSize;
    36. }
    37. else if (specMode == MeasureSpec.EXACTLY){
    38. // If your control can fit within these bounds return that value.
    39. result = specSize;
    40. }
    41. return result;
    42. }

[转]Android View.onMeasure方法的理解的更多相关文章

  1. Android View.onMeasure方法的理解

    View在屏幕上显示出来要先经过measure(计算)和layout(布局).1.什么时候调用onMeasure方法? 当控件的父元素正要放置该控件时调用.父元素会问子控件一个问题,“你想要用多大地方 ...

  2. [转载]Android View.onMeasure方法的理解

    2013-12-18 10:56:28 转载自http://blog.sina.com.cn/s/blog_61fbf8d10100zzoy.html View在屏幕上显示出来要先经过measure( ...

  3. Android View.onMeasure方法的理解(转载)

    一下内容转载自http://blog.sina.com.cn/s/blog_61fbf8d10100zzoy.html View在屏幕上显示出来要先经过measure(计算)和layout(布局).1 ...

  4. Android之View.onMeasure方法

    View在屏幕上显示出来要先经过measure(计算)和layout(布局). 1.什么时候调用onMeasure方法? 当控件的父元素正要放置该控件时调用.父元素会问子控件一个问题,“你想要用多大地 ...

  5. Android的onMeasure方法

    在Android开发中,当Android原生控件不能满足我们的需求的时候,就需要自定义View.View在屏幕上绘制出来先要经过measure(计算)和layout(布局). 什么时候调用onMeas ...

  6. android自定义控件onMeasure方法

    1.自定义控件首先定义一个类继承View 有时,Android系统控件无法满足我们的需求,因此有必要自定义View.具体方法参见官方开发文档:http://developer.android.com/ ...

  7. 谈谈我对Android View事件分发的理解

    写这篇博客的缘由.近期因为项目中用到相似一个LinearLayout中水平布局中,有一个TextView和Button,然后对该LinearLayout布局设置点击事件.点击TextView能够触发该 ...

  8. Android View post 方法

    解析View.post方法.分析一下这个方法的流程. 说起post方法,我们很容易联想到Handler的post方法,都是接收一个Runnable对象.那么这两个方法有啥不同呢? Handler的po ...

  9. Android View 测量流程(Measure)完全解析

    前言 上一篇文章,笔者主要讲述了DecorView以及ViewRootImpl相关的作用,这里回顾一下上一章所说的内容:DecorView是视图的顶级View,我们添加的布局文件是它的一个子布局,而V ...

随机推荐

  1. iOS学习21之UILabel, UITextField, UIButton, UIImageView

    1.UILabel 1> 概述 UILabel (标签): 是显示文本的控件.在App中 UILabel 是出现频率最高的控件 UILabel 是 UIView 子类,作为子类一般是为了扩充父类 ...

  2. JS学习(二)

    1 数组对象 作用是:使用单独的变量名来存储一系列的值. 1.1 新建数组: var mycars = new Array() mycars[0] = "Saab" mycars[ ...

  3. jQuery取得select选中的值

    $("#sxselect").change(function(){ alert($("#sxselect option:selected").val()); } ...

  4. CSS_css sprite原理优缺点及使用

    CSS Sprites在国内很多人叫css精灵,是一种网页图片应用处理方式.它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问该页面时,载入的图片就不会像以前那样一幅一幅地慢 ...

  5. 修改linux运行级别

    1.Linux下的7个运行级别 0 系统停机模式,系统默认运行级别不能设置为0,否则不能正常启动,机器关闭. 1 单用户模式,root权限,用于系统维护,禁止远程登陆,就像Windows下的安全模式登 ...

  6. 在IE8中使用padding设置select控件文字垂直居中

    在火狐.苹果.谷歌.欧鹏等主流浏览器中,select下拉表单的文字能够垂直居中,如图: 而在ie8中,select下拉表单的文字基本就是靠底部显示,如图: 那么,如何使得ie8下的select文字垂直 ...

  7. linux recv 返回值与linux socket 错误分析

    转载:http://blog.csdn.net/henry115/article/details/7054603 recv函数 int recv( SOCKET s, char FAR *buf, i ...

  8. [LintCode] Intersection of Two Arrays 两个数组相交

    Given two arrays, write a function to compute their intersection.Notice Each element in the result m ...

  9. android-BaseAdapter自定义控件深刻理解

    一.自定义控件的实现 自定义控件需要继承BaseAdapter抽象类,该类实现了ListAdapter, SpinnerAdapter两个接口,这两个接口继承了Adapter接口类,没错.是继承Ada ...

  10. POJ 1195 二维树状数组

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 18489   Accepted: 8558 De ...