1. 程式人生 > >onLayout onMeasure

onLayout onMeasure

View的measure方法會呼叫自己的onMeasure方法。
View要在onMeasure中計算自己的大小,並將結果通過呼叫setMeasuredDimension(int width, int height)來儲存結果。
同時也要呼叫子View的measure方法要求子View去計算自己的大小。呼叫子View的measure方法時傳的引數要根據當前onMeasure接收的引數和子View的layout_width、layout_height屬性。當layout_width屬性是wrap_content時應該傳AT_MOST和從onMeasure方法得到的width。當layout_width屬性是match_parent時應該傳EXACTLY和從onMeasure方法得到的width。當layout_width屬性是固定值時應該傳EXACTLY和layout_width對應的固定值(如果該固定值超過從onMeasure方法得到的width則傳後者)。
計算結果可以通過getMeasuredWidth()和getMeasuredHeight()獲取。


當一個控制元件的Layout方法被呼叫後geiWidth和getHeight 方法返回值才不會是0。
View的layout方法會被父View呼叫。
View的layout方法會呼叫自己的onLayout方法。
View要在onLayout方法中呼叫子View的layout方法去放置子View。