android View类及View派生类的 measure/layout 相关方法实现 调查

来源:互联网 发布:淘宝电话改差评技巧 编辑:程序博客网 时间:2024/05/08 12:10

View:


public final void measure(int widthMeasureSpec, int heightMeasureSpec) {//有实现,final的 祖祖辈辈用一套}这个方法注释也建议子类不要覆盖, 覆盖onLayout()     * 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.public void layout(int l, int t, int r, int b) {//有实现}protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {      // 无脑实现      setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),            getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));}protected void onLayout(boolean changed, int left, int top, int right, int bottom) {     //毛都没有}

ViewGroup extends View:

ViewGroup是一个抽象类,唯一的一个abstract方法就是onLayout().

使用父类的measure() 和 onMeasure()// 又被覆盖成abstarct了....,为了保证后面viewGroup派生类实现自己的排布法,也是一个提醒。@Overrideprotected abstract void onLayout(boolean changed,            int l, int t, int r, int b);@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;        }}


FrameLayout extends ViewGroup:

measure 和 layout 继承父类实现,实现了自己的onMeasure() 和 onLayout(),这也是android所希望的方式,自己的定制化实现都放在onMeasure() 和 onLayout()中.


LinearLayout extends ViewGroup:

measure 和 layout 继承父类实现,实现了自己的onMeasure() 和 onLayout(),这也是android所希望的方式,自己的定制化实现都放在onMeasure() 和 onLayout()中.

AdapterView extends ViewGroup:

另外,AdapterView因为定位于Adapter来驱动视图变化,因此他的所有Add/RemoveView() 都不可用,会抛异常.

可用的是Add/RemoveViewInLayout()<不会触发requestLayout>

    measure() onMeasure() layout() 继承父类ViewGroup        @Override    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {         // 简单实现         mLayoutHeight = getHeight();    }

AbsListView extends AdapterView<ListAdapter>

    measure() layout() 继承父类AdapterView<ListAdapter>自己实现onMeasure() 和 onLayout() 

ViewGroup 里面有几个方法:

protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) 

protected void measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec)

protected void measureChildWithMargins(View child,  int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed)

这几个方法,ViewGroup本身没有使用,是留给子类使用的.


invalidate() 一般不会触发 onLayout 和 onMeasure (除非之前forceLayout过), 而Layout()中的setFrame() 在尺寸变化的时候会调用 invalidate() (这只是其实一例,还有别的用invalidate的情况).

这几个方法是在 ViewGroup的实现类 的onMeasure()中被使用的,ViewGroup本身和View不会使用.

0 0
原创粉丝点击