onMeasure和onLayout

可以参照View.java中

Implementing a Custom View

的介绍

1. onMeasure: determine the size requirements for this view and all of its children.

a. 属于View的方法,用来测量自己及其子元素来确定宽度和高度

b. view的measure方法体中会调用onMeasure

c. onMeasure中必须确定 measured width and height of this view, 调用 setMeasuredDimension(int, int)或者onMeasure(int, int)

CONTRACT: When overriding this method, you must callsetMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger anIllegalStateException, thrown bymeasure(int, int). Calling the superclass'onMeasure(int, int) is a valid use.

The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should overrideonMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).



2. onLayout:when this view should assign a size and position to all of its children

a. 属于ViewGroup的方法,用来为当前ViewGroup的子元素的位置和大小(大小是layout分配给本元素的显示空间大小,与本元素的大小可能不一样)

b. View的layout方法体中会调用onLayout


3.onMeasure和onLayout

onMeasure在onLayout之前调用

 

4. 设置background后,会重新调用onMeasure和onLayout

 

5. 如果没有直接调用measure并且指定参数,onMeasure的参数是怎么得到的呢?

你可能感兴趣的:(android)