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()。

 
 

 
 

最新文章

  1. MVC使用内建的Form辅助器方法创建Select元素
  2. HRS(CRLF Injection)
  3. Extract Stylish styles and save as JSON format
  4. UVALive - 6577 Binary Tree 递推+找规律
  5. jQuery遍历DOM
  6. JSP读取properties配置文件 解决读取中文乱码
  7. MySQL oracle 分页
  8. 王者荣耀_KEY
  9. Python模块学习---Web
  10. Python列表的深浅复制
  11. bootstrap网格设置等高度
  12. ios 中pickerView用法之国旗选择
  13. 【IDEA&amp;&amp;Eclipse】5、IntelliJ IDEA常见配置
  14. hadoop学习笔记壹 --环境搭建及配置文件的修改
  15. iOS 限制输入字数
  16. Codeforces 600E - Lomsat gelral 「$Dsu \ on \ tree$模板」
  17. [转] 机器学习是什么&mdash;&mdash;周志华
  18. vue组件懒加载(Load On Demand)
  19. ActiveX开发
  20. spring4-1-Spring的简单介绍

热门文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
  2. ajax 基础
  3. 在后台获取Textarea控件的值
  4. python中的异常如何处理
  5. U-Boot在FL2440上移植(四)----支持网卡DM9000和烧写yaffs文件系统
  6. ioctl函数详细说明
  7. genymotion下载出现Unable to create virtual device,Server returned HTTP status code 0.
  8. vs2010根据字符串内容添加断点
  9. python list comprehension twos for loop 嵌套for循环
  10. 一个简单的win32窗口