安卓开发学习之RelativeLayout的布局过程

背景

在文章Android开发学习之RelativeLayout测量流程源码阅读中,我记录了RelativeLayout的测量过程,其中它确定了每个子view的四个端点的位置,那在布局过程中,就可以直接使用了

onLayout

代码如下

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //  The layout has actually already been performed and the positions
        //  cached.  Apply the cached values to the children.
        final int count = getChildCount();

        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                RelativeLayout.LayoutParams st =
                        (RelativeLayout.LayoutParams) child.getLayoutParams();
                child.layout(st.mLeft, st.mTop, st.mRight, st.mBottom);
            }
        }
    }

由于测量过程已经把子view的四个端点确定,所以相对布局的onLayout()方法就非常简单,直接用测量的结果就可以


结语

由于相对布局和它的父类ViewGroup都没有覆写onDraw()方法,所以它的绘制流程,是在View中实现的,但View不是调用的onDraw()方法,因为这是个空实现,要交给子类实现,真正调用的,是View.draw(canvas)方法,详情参见参加安卓开发学习之View的draw(canvas)方法一文

猜你喜欢

转载自blog.csdn.net/qq_37475168/article/details/80318809