关于onLayout的学习,也是在基于View视图树的递归调用实现。
本篇想说明的是,不去深究View源码关于onLayout,以及layout方法的实现原理。知道大概,目的是在会用。但是需要了解并掌握View内部关于onLayout方法的相关API使用。
在ViewGroup中,onLayout是一个抽象方法,所以如果继承了ViewGroup类,除了添加构造方法外,还必须要重写onLayout方法。
/**
* 当这个view和其子view被分配一个大小和位置时,被layout调用。
* @param changed 当前View的大小和位置改变了
* @param left 左部位置(相对于父视图)
* @param top 顶部位置(相对于父视图)
* @param right 右部位置(相对于父视图)
* @param bottom 底部位置(相对于父视图)
*/
protected void onLayout(boolean changed, int left, int top, int right, int bottom)
此方法是在ViewGroup中的layout方法所调用:
@Override
public final void layout(int l, int t, int r, int b) {
if (!mSuppressLayout && (mTransition == null || !mTransition.isChangingLayout())) {
if (mTransition != null) {
mTransition.layoutChange(this);
}
super.layout(l, t, r, b);
} else {
// record the fact that we noop'd it; request layout when transition finishes
mLayoutCalledWhileSuppressed = true;
}
}
而V