本章主要讲述View的几个常用方法,对于后期自定义控件非常有用。
1、requestLayout()
在API文档中有这样的介绍:
Layout is a two pass process: a measure pass and a layout pass. The measuring pass is implemented in measure(int, int)......The second pass happens in layout(int, int, int, int)......
To intiate a layout, call requestLayout(). This method is typically called by a view on itself when it believes that is can no longer fit within its current bounds
因此,requestLayout()调用时,控件会重新执行 onMesure()和 onLayout()。
2、invalidate()
文档原话:To force a view to draw, call invalidate().
因此,invalidate()调用时,会执行onDraw()方法。
3、setMeasuredDimension (int measuredWidth, int measuredHeight)
文档说明:This method must be called by onMeasure(int, int) to store the measured width and measured height. Failing to do so will trigger an exception at measurement time.
该方法用于设置View的宽高,是在onMessure()调中执行(也就是说依赖于onMessure())。因此,对于View的宽高更改,我们可以重写onMessure(),并用setMeasuredDimension ()进行设置