View 绘制体系知识梳理(2) - setContentView 源码解析

一、概述

Activity当中,我们一般都会调用setContentView方法来初始化布局。

二、与ContentView相关的方法

Activity当中,与ContentView相关的函数有下面这几个,我们先看一下它们的注释说明:

    /**
     * Set the activity content from a layout resource.  The resource will be
     * inflated, adding all top-level views to the activity.
     *
     * @param layoutResID Resource ID to be inflated.
     *
     * @see #setContentView(android.view.View)
     * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
     */
    public void setContentView(@LayoutRes int layoutResID) {
        getWindow().setContentView(layoutResID);
        initWindowDecorActionBar();
    }

    /**
     * Set the activity content to an explicit view.  This view is placed
     * directly into the activity's view hierarchy.  It can itself be a complex
     * view hierarchy.  When calling this method, the layout parameters of the
     * specified view are ignored.  Both the width and the height of the view are
     * set by default to {@link ViewGroup.LayoutParams#MATCH_PARENT}. To use
     * your own layout parameters, invoke
     * {@link #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}
     * instead.
     *
     * @param view The desired content to display.
     *
     * @see #setContentView(int)
     * @see #setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
     */
    public void setContentView(View view) {
        getWindow().setContentView(view);
        initWindowDecorActionBar();
    }

    /**
     * Set the activity content to an explicit view.  This view is placed
     * directly into the activity's view hierarchy.  It can itself be a complex
     * view hierarchy.
     *
     * @param view The desired content to display.
     * @param params Layout parameters for the view.
     *
     * @see #setContentView(android.view.View)
     * @see #setContentView(int)
     */
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        getWindow().setContentView(view, params);
        initWindowDecorActionBar();
    }

    /**
     * Add an additional content view to the activity.  Added after any existing
     * ones in the activity -- existing views are NOT removed.
     *
     * @param view The desired content to display.
     * @param params Layout parameters for the view.
     */
    public void addContentView(View view, ViewGroup.LayoutParams params) {
        getWindow().addContentView(view, params);
        initWindowDecorActionBar();
    }

通过上面的注释,可以看到这4个方法的用途:

  • 第一种:渲染layouResId对应的布局,并将它添加到activity的顶级View中。
  • 第二种:将View添加到activity的布局当中,它的默认宽高都是ViewGroup.LayoutParams#MATCH_PARENT
  • 第三种:和上面相同,但是指定了LayoutParams
  • 第四种:将内容添加进去,并且必须指定LayoutParams,已经存在的View不会被移除。

这四种方法其实都是调用了PhoneWindow.java中的方法,通过源码我们可以发现setContentView(View view, ViewGroup.LayoutParams params)setContentView(@LayoutRes int layoutResID)的步骤基本上是一样的,只不过是在添加到布局的时候,前者因为已经获得了View的实例,因此用的是addView的方法,而后者因为需要先inflate,所以,使用的是LayoutInflater

三、setContentView方法

下面我们以setContentView(@LayoutRes int layoutResID)为例,看一下具体的实现步骤:

3.1 setContentView

    @Override
    public void setContentView(int layoutResID) {
        // Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
        // decor, when theme attributes and the like are crystalized. Do not check the feature
        // before this happens.
        if (mContentParent == null) {
            installDecor();
        } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
            mContentParent.removeAllViews();
        }

        if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
            final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
                    getContext());
            transitionTo(newScene);
        } else {
            mLayoutInflater.inflate(layoutResID, mContentParent);
        }
        mContentParent.requestApplyInsets();
        final Callback cb = getCallback();
        if (cb != null && !isDestroyed()) {
            cb.onContentChanged();
        }
        mContentParentExplicitlySet = true;
    }

首先,我们会判断mContentParent是否为空,通过添加的代码我们可以知道,这个mContentParent其实就是layoutResId最后渲染出的布局所对应的父容器,当这个ContentParent为空时,调用了installDecormContentParent就是在里面初始化的。

3.2 installDecor()

    private void installDecor() {
        //如果DecorView不存在,那么先生成它,它其实是一个FrameLayout。
        if (mDecor == null) {
            mDecor = generateDecor();
        }
        //如果`ContentParent`不存在,那么也生成它,此时传入了前面的`DecorView`
        if (mContentParent == null) {
            mContentParent = generateLayout(mDecor);
            final DecorContentParent decorContentParent = (DecorContentParent) mDecor.findViewById(R.id.decor_content_parent);
            if (decorContentParent != null) {
                mDecorContentParent = decorContentParent;
            }
        }
    }

我们可以看到,mDecor是一个FrameLayout,它和mContentParent的关系是通过mContentParent = generateLayout(mDecor)产生。

3.3 generateLayout(DecorView decor)

    protected ViewGroup generateLayout(DecorView decor) {
        //...首先根据不同的情况,给`layoutResource`赋予不同的值.
        View in = mLayoutInflater.inflate(layoutResource, null);
        decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
        mContentRoot = (ViewGroup) in;
        ViewGroup contentParent = (ViewGroup) findViewById(ID_ANDROID_CONTENT);
        if (contentParent == null) {
            throw new RuntimeException("Window couldn't find content container view");
        }
        //...
        return contentParent;
    }

在上面赋值的过程中,我们主要关注以下几个变量,mContentRoot/mContentParent/mDecorContent

  • mContentRoot一定是mDecor的下一级子容器。
  • mContentParentmDecor当中idR.id.contentViewGroup,但是它mDecor的具体层级关系不确定,这依赖于mContentRoot是通过哪个xml渲染出来。
  • mContentParent一定是传入的layoutResId进行 inflate完成之后的父容器,它一定不会为空,否则会抛出异常,我们setContentView(xxx)方法传入的布局,就是它的子View
    // This is the top-level view of the window, containing the window decor.
    private DecorView mDecor;

    // This is the view in which the window contents are placed. It is either
    // mDecor itself, or a child of mDecor where the contents go.
    private ViewGroup mContentParent;
  • mDecorContent则是mDecor当中iddecor_content_parentViewGroup,但是也有可能mDecor
    当中没有这个idView,这需要依赖与我们的mContentRoot是使用了哪个xmlinflate的。

再回到前面setContentView的地方,继续往下看,mContentParent不为空的时候,那么会移除它底下的所有子View
之后会调用mLayoutInflater.inflate(layoutResID, mContentParent);方法,把传入的View添加到mContentParent当中,最后回调一个监听。

final Callback cb = getCallback();
if (cb != null && !isDestroyed()) {
    cb.onContentChanged();
}

3.4 mContentParentExplicitlySet标志位

setContentView的最后,将mContentParentExplicitlySet这个变量设置为true,这个变量其实是用在requestFeature当中,也就是说,我们必须在调用setContentView之前,调用requestFeature,否则就会抛出下面的异常:

    @Override
    public boolean requestFeature(int featureId) {
        if (mContentParentExplicitlySet) {
            throw new AndroidRuntimeException("requestFeature() must be called before adding content");
        }
        return super.requestFeature(featureId);
    }

因此:requestFeature(xxx)必须要在调用setContentView(xxx)之前

三、addContentView(View view, ViewGroup.LayoutParams params)

下面我们再来看一下,addContentView方法:

    @Override
    public void addContentView(View view, ViewGroup.LayoutParams params) {
        if (mContentParent == null) {
            installDecor();
        }
        mContentParent.addView(view, params);
        mContentParent.requestApplyInsets();
        final Callback cb = getCallback();
        if (cb != null && !isDestroyed()) {
            cb.onContentChanged();
        }
    }

可以看到,它和set方法的区别就是,它在添加到mContentParent之前,并没有把mContentParent的所有子View都移除,而是将它直接添加进去,通过布局分析软件,可以看到mContentParent的类型为ContentFrameLayout,它其实是一个FrameLayout,因此,它会覆盖在mContentParent已有子View之上

四、将添加的布局和ActivityWindow关联起来

在上面的分析当中,我们仅仅是初始化了一个DecorView,并根据设置的Style属性,传入的ContentView来初始化它的子布局,但是这时候它还有真正和ActivityWindow关联起来,关联的地方在ActivityThread.java中:

final void handleResumeActivity(IBinder token,
            boolean clearHide, boolean isForward, boolean reallyResume, int seq, String reason) {
        r = performResumeActivity(token, clearHide, reason);
        if (r != null) {
            if (r.window == null && !a.mFinished && willBeVisible) {
                r.window = r.activity.getWindow();
                View decor = r.window.getDecorView();
                decor.setVisibility(View.INVISIBLE);
                ViewManager wm = a.getWindowManager();
                WindowManager.LayoutParams l = r.window.getAttributes();
                a.mDecor = decor;
                l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
                l.softInputMode |= forwardBit;
                if (r.mPreserveWindow) {
                    a.mWindowAdded = true;
                    r.mPreserveWindow = false;
                    // Normally the ViewRoot sets up callbacks with the Activity
                    // in addView->ViewRootImpl#setView. If we are instead reusing
                    // the decor view we have to notify the view root that the
                    // callbacks may have changed.
                    ViewRootImpl impl = decor.getViewRootImpl();
                    if (impl != null) {
                        impl.notifyChildRebuilt();
                    }
                }
                if (a.mVisibleFromClient && !a.mWindowAdded) {
                    a.mWindowAdded = true;
                    wm.addView(decor, l);
                }
            } else if (!willBeVisible) {
                if (localLOGV) Slog.v(
                    TAG, "Launch " + r + " mStartedActivity set");
                r.hideForNow = true;
            }
        } else {

        }
    }

从源码中可以看到,如果在执行handleResumeActivity时,之前DecorView没有被添加到WindowManager当中时,那么它的第一次添加是在onResume()方法执行完之后添加的

    原文作者:泽毛
    原文地址: https://www.jianshu.com/p/ee98bab25fdf
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞