View加载的流程之测量:rootView调用measure()→onMeasure();

measure()是final方法,表明Android不想让开发者去修改measure的框架,开发者可以onMeasure方法。

来看一下measure的代码:

  • public final void measure(int widthMeasureSpec, int heightMeasureSpec) {  
  •     if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||  
  •             widthMeasureSpec != mOldWidthMeasureSpec ||  
  •             heightMeasureSpec != mOldHeightMeasureSpec) {  
  •         mPrivateFlags &= ~MEASURED_DIMENSION_SET;  
  •         if (ViewDebug.TRACE_HIERARCHY) {  
  •             ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);  
  •         }  
  •         onMeasure(widthMeasureSpec, heightMeasureSpec);  
  •         if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {  
  •             throw new IllegalStateException("onMeasure() did not set the"  
  •                     + " measured dimension by calling"  
  •                     + " setMeasuredDimension()");  
  •         }  
  •         mPrivateFlags |= LAYOUT_REQUIRED;  
  •     }  
  •     mOldWidthMeasureSpec = widthMeasureSpec;  
  •     mOldHeightMeasureSpec = heightMeasureSpec;  
  • }  

     
     

 
 

一个界面的展示可能会涉及到很多次发measure,因为一个视图往往包含多个子视图,每个视图都需要经历一次measure过程。ViewGroup中定义了measureChildren()方法来测量子视图,下面是measureChildren():

 
 

  • protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) {  
  •     final int size = mChildrenCount;  
  •     final View[] children = mChildren;  
  •     for (int i = 0; i < size; ++i) {  
  •         final View child = children[i];  
  •         if ((child.mViewFlags & VISIBILITY_MASK) != GONE) {  
  •             measureChild(child, widthMeasureSpec, heightMeasureSpec);  
  •         }  
  •     }  
  • }  

 
 

View加载的第二步: onLayout,这和measure差不多,都是由rootView调用layout()→onLayout()。

 
 

 
 

Android学习笔记之View(二)的更多相关文章

  1. 【转】 Pro Android学习笔记(九二):AsyncTask(1):AsyncTask类

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在Handler的学习系列中,学习了如何h ...

  2. 【转】 Pro Android学习笔记(五二):ActionBar(5):list模式

    可以在action bar中加入spinner的下来菜单,有关spinner,可以参考Pro Android学习笔记(二十):用户界面和控制(8):GridView和Spinner. list的样式和 ...

  3. 【转】 Pro Android学习笔记(四二):Fragment(7):切换效果

    目录(?)[-] 利用setTransition 利用setCustomAnimations 通过ObjectAnimator自定义动态效果 程序代码的编写 利用fragment transactio ...

  4. 【转】Pro Android学习笔记(十二):了解Intent(下)

    解析Intent,寻找匹配Activity 如果给出component名字(包名.类名)是explicit intent,否则是implicit intent.对于explicit intent,关键 ...

  5. 【转】 Pro Android学习笔记(八二):了解Package(1):包和进程

    文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件.转载须注明出处:http://blog.csdn.net/flowingflying/ 在之前,我们已经学习了如何签发apk,见P ...

  6. Android学习笔记(十二)——实战:制作一个聊天界面

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! 运用简单的布局知识,我们可以来尝试制作一个聊天界面. 一.制作 Nine-Patch 图片 : Nine-Pa ...

  7. 【转】 Pro Android学习笔记(六二):Preferences(6):header

    目录(?)[-] 代码实现 header xml文件 在前面的例子,我们主要学习了PreferenceScreen的xml如何写,preference有哪些类型.在代码中,我们为了不提示warning ...

  8. 【转】 Pro Android学习笔记(三二):Menu(3):Context菜单

    目录(?)[-] 什么是Context menu 注册View带有Context menu 填Context菜单内容 Context菜单点击触发 什么是Context menu 在桌面电脑,我们都很熟 ...

  9. 【转】 Pro Android学习笔记(七二):HTTP服务(6):HttpURLConnection

    目录(?)[-] Http Get的使用方式 基础小例子 Cookie的使用 重定向 HTTP POST的小例子 基础小例子 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件,转载 ...

随机推荐

  1. .net c# 提交包含文件file 的form表单 获得文件的Stream流

    1.前台html代码 要写一个有id的form,可是不能有runat="server"属性.由于一个页面中,有这个属性的form表单仅仅能有一个. 再要有一个有name的ifram ...

  2. leetcode_question_70 Climbing Stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  3. iOS开发UI 篇—CAlayer层的属性

    一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property CGPoint position; 用来设 ...

  4. [Swust OJ 772]--Friend(并查集+map的运用)

    题目链接:http://acm.swust.edu.cn/problem/772/ Time limit(ms): 1000 Memory limit(kb): 65535    Descriptio ...

  5. Python之美[从菜鸟到高手]--urlparse源码分析

    urlparse是用来解析url格式的,url格式如下:protocol :// hostname[:port] / path / [;parameters][?query]#fragment,其中; ...

  6. Yii2 国际化的问题 zh-CN

    代码增加位置: 在项目文件目录的config->main.php 的 return 内增加以下内容,并在项目目录中新建messages 内设置 en-US 和zh-CN 文件夹.zh-CN文件夹 ...

  7. Kqueue与epoll机制

    首先介绍阻塞与非阻塞:阻塞是个什么概念呢?比如某个时候你在等快递,但是你不知道快递什么时候过来,而且你没有别的事可以干(或者说接下来的事要等快递来了才能做):那么你可以去睡觉了,因为你知道快递把货送来 ...

  8. 配置 .vimrc 解决 Vim / gVim 在中文 Windows 下的字符编码问题

    转载自:-杨博的日志 - 网易博客 Vim / gVim 在中文 Windows 下的字符编码有两个问题: 默认没有编码检测功能 如果一个文件本身采用的字符集比 GBK 大(如 UTF-8.UTF-1 ...

  9. What is Webhook ( Introduction to Webhook )

    A webhook in web development is a method of augmenting or altering the behavior of a web page, or we ...

  10. 6月10日-IOS应用-日记本

    嗯,经过这几天的学习,我的第一个IOS应用,日记本算是学习完毕了,下面写一篇日记,记录所学到的知识和需要继续学习的地方. 1,首先是ViewController,必须添加两个协议UITableView ...