@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之后。
?