设为首页 加入收藏

TOP

getWidth和getMeasuredWidth在何时可以得到正确数值(二)
2015-07-20 17:59:54 来源: 作者: 【 】 浏览:8
Tags:getWidth getMeasuredWidth 何时 可以 得到 正确 数值
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { // TODO Auto-generated method stub super.onLayout(changed, left, top, right, bottom); Log.i(AAA, TextView onLayout Width:+getWidth() + Height:+getHeight()); Log.i(AAA, TextView onLayout MeasuredWidth:+getMeasuredWidth() + MeasuredHeight:+getMeasuredHeight()); } } }
运行之后得到的结果如下:

?

I/AAA ( 3631): TextView MTextView Width:0 Height:0
I/AAA ( 3631): TextView MTextView MeasuredWidth:0 MeasuredHeight:0
I/AAA ( 3631): LinearLayout MLinearLayout Width:0 Height:0
I/AAA ( 3631): LinearLayout MLinearLayout MeasuredWidth:0 MeasuredHeight:0
I/AAA ( 3631): TextView onMeasure Width:0 Height:0
I/AAA ( 3631): TextView onMeasure MeasuredWidth:200 MeasuredHeight:300
I/AAA ( 3631): LinearLayout onMeasure Width:0 Height:0
I/AAA ( 3631): LinearLayout onMeasure MeasuredWidth:1024 MeasuredHeight:502
I/AAA ( 3631): TextView onLayout Width:200 Height:300
I/AAA ( 3631): TextView onLayout MeasuredWidth:200 MeasuredHeight:300
I/AAA ( 3631): LinearLayout onLayout Width:1024 Height:502
I/AAA ( 3631): LinearLayout onLayout MeasuredWidth:1024 MeasuredHeight:502
I/AAA ( 3631): TextView onMeasure Width:200 Height:300
I/AAA ( 3631): TextView onMeasure MeasuredWidth:200 MeasuredHeight:300
I/AAA ( 3631): LinearLayout onMeasure Width:1024 Height:502
I/AAA ( 3631): LinearLayout onMeasure MeasuredWidth:1024 MeasuredHeight:502
I/AAA ( 3631): TextView onLayout Width:200 Height:300
I/AAA ( 3631): TextView onLayout MeasuredWidth:200 MeasuredHeight:300
I/AAA ( 3631): LinearLayout onLayout Width:1024 Height:502
I/AAA ( 3631): LinearLayout onLayout MeasuredWidth:1024 MeasuredHeight:502

结论:

1.在构造方法中无论是getWidth还是getMeasuredWidth都是得不到正确数值的。

2.getMeasuredWidth得到正确数值需要在调用过onMeasure之后。

3.getWidth得到正确数值需要调用过onLayout之后。

?

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇算法怎么就这么难---归并排序和快.. 下一篇ListView中pointToPosition()方法..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: