android measure

首先 onMeasure方法是为了得到各个View大小的函数

 

fill_parent-->public static final int EXACTLY = 1 << MODE_SHIFT;

wrap_content-->public static final int AT_MOST = 2 << MODE_SHIFT;

 

 

  

[c-sharp] view plain copy print ?
  1. public static int makeMeasureSpec(int size, int mode) {  
  2.            return size + mode;  
  3.        }  
 

 

[c-sharp] view plain copy print ?
  1. /** 
  2.   * Special value for the height or width requested by a View. 
  3.   * FILL_PARENT means that the view wants to fill the available space 
  4.   * within the parent, taking the parent's padding into account. 
  5.   */  
  6.  public static final int FILL_PARENT = -1;  
  7.  /** 
  8.   * Special value for the height or width requested by a View. 
  9.   * WRAP_CONTENT means that the view wants to be just large enough to fit 
  10.   * its own internal content, taking its own padding into account. 
  11.   */  
  12.  public static final int WRAP_CONTENT = -2;  
 

我想应该是在解析XML的时候 FILL_PARENT=-(-1)>>30 WRAP_CONTENT=-(-2>>30) 猜测。。没找到证据。。。有谁能找到根我说句。。

 

通过上面的code就可以得到对应的 onMeasuere(int Widthspec,int HeighSpec)中对应的大小

比如我定义一个xml

 

 android:layout_width="200px"

 android:layout_height="500px"

对应的到的widhthSpec为EXACTLY +200;对应的HeightSpec为EXACTLY +500;

如果android:layout_width="wrap_content"-->widhtSpec=AT_MOST 

 

 android:layout_height="fill_parent"-->HeightSpec=EXACTLY

因为都会调用makeMeasureSpec来确定大小。比如在ViewGroup中的源码返回的

return MeasureSpec.makeMeasureSpec(resultSize, resultMode);

 

//这段代码太有才了。

 

public static int getSize(int measureSpec) {

return (measureSpec & ~MODE_MASK);

}

 

举个例子:

 

11&~10=1

2&~10=2

意思就是超过10的部分就用超过的部分比如11-10=1

没超过10的就用本事。比如2

这样的话,就可以用android:layout_width="200px" 和fill_parent等属性。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值