各位看官们,大家好,上一回中咱们说的是Android中自定义View的例子,这一回咱们继续说该例子。闲话休提,言归正转。让我们一起Talk Android吧!
看官们,我们在上一回是介绍了如何重写测量功能,这一回中我们将介绍如果重写规则功能。我们在前面章回是介绍过,规划的主要功能是通过layout()
函数实现的,我们想自定义View就需要在子类中重写该函数,但是遗憾的是该函数不能被重写,“其原因和onMeasure()函数一样”。这是哪位看官说的?它的函数原型里可没有final哟,我们看看它的函数原型
public void layout(int l, int t, int r, int b) {}
那是什么原因呢?其实是google不建议我们去重写该函数,而是重写onLayout()
函数。这点在该函数的注释中专门进行
了提示:
* <p>Derived classes should not override this method.
* Derived classes with children should override
* onLayout. In that method, they should
* call layout on each of their children.</p>
至于如何重写,我们在后面的例子中再做详细介绍。目前,大家只要知道,要在自定义的View中修改规划功能,那么就去重写 onLayout()
函数。下面是它的函数原型:
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {}
各位看官,关于Androd中自定义View的例子咱们就介绍到这里,欲知后面还有什么例子,且听下回分解!