onMeasure(int widthMeasureSpec, int heightMeasureSpec)

来源:互联网 发布:大数据 风险管理 编辑:程序博客网 时间:2024/05/01 06:38
  1. 除非你总是需要一个100×100像素的控件,否则,你必须要重写onMeasure。  
  2.   
  3. onMeasure方法在控件的父元素正要放置它的子控件时调用。它会问一个问题,“你想要用多大地方啊?”,然后传入两个参数——widthMeasureSpec和heightMeasureSpec。  
  4. 它们指明控件可获得的空间以及关于这个空间描述的元数据。  
  5.   
  6.  比返回一个结果要好的方法是你传递View的高度和宽度到setMeasuredDimension方法里。  
  7. 接下来的代码片段给出了如何重写onMeasure。注意,调用的本地空方法是来计算高度和宽度的。它们会译解widthHeightSpec和heightMeasureSpec值,并计算出合适的高度和宽度值。  
  8.   
  9.    
  10.   
  11. @Override  
  12.   
  13. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  14.   
  15. int measuredHeight = measureHeight(heightMeasureSpec);  
  16.   
  17. int measuredWidth = measureWidth(widthMeasureSpec);  
  18.   
  19. setMeasuredDimension(measuredHeight, measuredWidth);  
  20.   
  21. }  
  22.   
  23.    
  24.   
  25. private int measureHeight(int measureSpec) {  
  26.   
  27. // Return measured widget height.  
  28.   
  29. }  
  30.   
  31.    
  32.   
  33. private int measureWidth(int measureSpec) {  
  34.   
  35. // Return measured widget width.  
  36.   
  37. }  
  38.   
  39.    
  40.   
  41. 边界参数——widthMeasureSpec和heightMeasureSpec ,效率的原因以整数的方式传入。在它们使用之前,首先要做的是使用MeasureSpec类的静态方法getMode和getSize来译解,如下面的片段所示:  
  42.   
  43.    
  44.   
  45. int specMode = MeasureSpec.getMode(measureSpec);  
  46.   
  47. int specSize = MeasureSpec.getSize(measureSpec);  
  48.   
  49.    
  50.   
  51. 依据specMode的值,如果是AT_MOST,specSize 代表的是最大可获得的空间;如果是EXACTLY,specSize 代表的是精确的尺寸;如果是UNSPECIFIED,对于控件尺寸来说,没有任何参考意义。  
  52.   
  53. 当以EXACT方式标记测量尺寸,父元素会坚持在一个指定的精确尺寸区域放置View。在父元素问子元素要多大空间时,AT_MOST指示者会说给我最大的范围。在很多情况下,你得到的值都是相同的。  
  54.   
  55. 在两种情况下,你必须绝对的处理这些限制。在一些情况下,它可能会返回超出这些限制的尺寸,在这种情况下,你可以让父元素选择如何对待超出的View,使用裁剪还是滚动等技术。  
  56.   
  57.  接下来的框架代码给出了处理View测量的典型实现:  
  58.   
  59.    
  60.   
  61. @Override  
  62.   
  63. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  64.   
  65. int measuredHeight = measureHeight(heightMeasureSpec);  
  66.   
  67. int measuredWidth = measureWidth(widthMeasureSpec);  
  68.   
  69. setMeasuredDimension(measuredHeight, measuredWidth);  
  70.   
  71. }  
  72.   
  73.    
  74.   
  75. private int measureHeight(int measureSpec) {  
  76.   
  77. int specMode = MeasureSpec.getMode(measureSpec);  
  78.   
  79. int specSize = MeasureSpec.getSize(measureSpec);  
  80.   
  81.    
  82.   
  83. // Default size if no limits are specified.  
  84.   
  85. int result = 500;  
  86.   
  87. if (specMode == MeasureSpec.AT_MOST)   
  88.   
  89. {  
  90.   
  91. // Calculate the ideal size of your  
  92.   
  93. // control within this maximum size.  
  94.   
  95. // If your control fills the available  
  96.   
  97. // space return the outer bound.  
  98.   
  99. result = specSize;  
  100.   
  101. }   
  102.   
  103. else if (specMode == MeasureSpec.EXACTLY)   
  104.   
  105. {  
  106.   
  107. // If your control can fit within these bounds return that value.  
  108.   
  109. result = specSize;  
  110.   
  111. }  
  112.   
  113. return result;  
  114.   
  115. }  
  116.   
  117.    
  118.   
  119. private int measureWidth(int measureSpec) {  
  120.   
  121. int specMode = MeasureSpec.getMode(measureSpec);  
  122.   
  123. int specSize = MeasureSpec.getSize(measureSpec);  
  124.   
  125.    
  126.   
  127. // Default size if no limits are specified.  
  128.   
  129. int result = 500;  
  130.   
  131. if (specMode == MeasureSpec.AT_MOST)  
  132.   
  133. {  
  134.   
  135. // Calculate the ideal size of your control  
  136.   
  137. // within this maximum size.  
  138.   
  139. // If your control fills the available space  
  140.   
  141. // return the outer bound.  
  142.   
  143. result = specSize;  
  144.   
  145. }   
  146.   
  147. else if (specMode == MeasureSpec.EXACTLY)   
  148.   
  149. {  
  150.   
  151. // If your control can fit within these bounds return that value.  
  152.   
  153. result = specSize;  
  154.   
  155. }  
  156.   
  157. return result;  
  158.   
  159. }  
  160.   
2 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 华迈净水器漏水怎么办 超限超载告知书怎么办 辽宁省荣复军人怎么办 加载失败网页打不开怎么办 整容证明开不了怎么办 无法开整容证明怎么办 整容后换身份证怎么办 换身份证了驾照怎么办 网易云安装不了怎么办 上网站看不到东西怎么办 电脑显示应用程序错误怎么办 电脑出现应用程序错误怎么办 老师教错了怎么办 老师不让坐位上怎么办 老师问学生家长借钱怎么办 学生向老师借钱怎么办 老师向家长借钱怎么办 癌症身上很痒怎么办 花生地有老鼠怎么办 国企不涨工资怎么办 mpacc写作写不好怎么办 宝宝惊着了怎么办 半夜到昆明机场怎么办 离婚了老公纠缠怎么办 网络写手侵权怎么办 etc卡不能用怎么办 在海上遇到大浪怎么办 数学太差了怎么办 四年级数学成绩差怎么办 小学科学成绩差怎么办 小学科学成绩不好怎么办 高中数理化学不好怎么办 初中生长痘痘怎么办 怎么办朱杰mp3微盘 摄像机镜头起雾怎么办 显卡风扇声音大怎么办 卧室灯不在中间怎么办 小龙头发乱了怎么办 孩子头睡平了怎么办 一只眼失明找不到工作怎么办 幼儿园的小朋友不听话怎么办