Activity的setContentView()源码分析

作者: Android天之骄子 | 来源:发表于2017-08-23 17:35 被阅读51次

    阅读setContentView()源码的疑问

    首先读源码要带着自己的疑问来读源码,setContentView()到底做了什么,为什么调用后就可以显示出我们想要的布局页面?getWindow().requestFeature(Window.FEATURE_NO_TITLE);为什么要在setContentView()之前调用,下面我就就带着这些疑问来一探究竟。

         /**
         * 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();
        }
    

    进入到setContentView(),我们发现调用的是getWindow()的setContentView()方法,getWindow()其实就是一个Window,Window是一个抽象类,从注释我们可以知道Window的唯一实现类是PhoneWindow。

    /**
     * Abstract base class for a top-level window look and behavior policy.  An
     * instance of this class should be used as the top-level view added to the
     * window manager. It provides standard UI policies such as a background, title
     * area, default key processing, etc.
     *
     * <p>The only existing implementation of this abstract class is
     * android.view.PhoneWindow, which you should instantiate when needing a
     * Window.
     */
    public abstract class Window 
    

    也就是说Activity的setContentView()调用的是PhoneWindow的setContentView(),我们再看下PhoneWindow的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();  //1
            } 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);   //2
            }
          ...
        }
    

    在这里会对mContentParent进行判空,mContentParent是一个ViewGroup,从名字和代码②mLayoutInflater.inflate(layoutResID, mContentParent); 我们可以先猜测一下Activity要显示的布局是不是就加载到mContentParent中呢?我们先留个疑问,稍后分析inflate()。

        // 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.
        ViewGroup mContentParent;
    

    注释:这是放置窗口内容的视图。 它是mDecor本身,或mDecor的一个孩子。
    当mContentParent为null时,就会执行代码① installDecor();我们再进入到 installDecor()。

        private void installDecor() {
            mForceDecorInstall = false;
            if (mDecor == null) {
                mDecor = generateDecor(-1);
                mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
                mDecor.setIsRootNamespace(true);
                if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
                    mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
                }
            } else {
                mDecor.setWindow(this);
            }
            if (mContentParent == null) {
                mContentParent = generateLayout(mDecor);              //*********3**********
                ...
                }
            }
        }
    

    当我们新启动一个Activity是mDecor 和 mContentParent 都是为空的,分别看下generateDecor()和generateLayout(mDecor)都做了什么。

    创建generateDecor

    protected DecorView generateDecor(int featureId) {
            // System process doesn't have application context and in that case we need to directly use
            // the context we have. Otherwise we want the application context, so we don't cling to the
            // activity.
            Context context;
            if (mUseDecorContext) {
                Context applicationContext = getContext().getApplicationContext();
                if (applicationContext == null) {
                    context = getContext();
                } else {
                    context = new DecorContext(applicationContext, getContext().getResources());
                    if (mTheme != -1) {
                        context.setTheme(mTheme);
                    }
                }
            } else {
                context = getContext();
            }
            return new DecorView(context, featureId, this, getAttributes());
        }
    

    这里就是创建一个DecorView,DecorView又是什么呢?DecorView继承于FrameLayout,在代码3处将创建的这个DecorView作为参数传到了generateLayout()中。

    创建generateLayout

    protected ViewGroup generateLayout(DecorView decor) {
            // Apply data from current theme.
            //获得style属性
            TypedArray a = getWindowStyle();
            ...
            ...
            if (a.getBoolean(R.styleable.Window_windowNoTitle, false)) {
                requestFeature(FEATURE_NO_TITLE);
            } else if (a.getBoolean(R.styleable.Window_windowActionBar, false)) {
                // Don't allow an action bar if there is no title.
                requestFeature(FEATURE_ACTION_BAR);
            }
    
            if (a.getBoolean(R.styleable.Window_windowActionBarOverlay, false)) {
                requestFeature(FEATURE_ACTION_BAR_OVERLAY);
            }
    
            if (a.getBoolean(R.styleable.Window_windowActionModeOverlay, false)) {
                requestFeature(FEATURE_ACTION_MODE_OVERLAY);
            }
    
            if (a.getBoolean(R.styleable.Window_windowSwipeToDismiss, false)) {
                requestFeature(FEATURE_SWIPE_TO_DISMISS);
            }
            ...
            // Inflate the window decor.
            int layoutResource;
            int features = getLocalFeatures();
            if ((features & (1 << FEATURE_SWIPE_TO_DISMISS)) != 0) {
                layoutResource = R.layout.screen_swipe_dismiss;
            } else if ((features & ((1 << FEATURE_LEFT_ICON) | (1 << FEATURE_RIGHT_ICON))) != 0) {
                    layoutResource = R.layout.screen_title_icons;
            } else {
                // Embedded, so no decoration is needed.
                layoutResource = R.layout.screen_simple;
                // System.out.println("Simple!");
            }
            ...
            mDecor.startChanging();    
            mDecor.onResourcesLoaded(mLayoutInflater, layoutResource);   
    
            ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);    //*****4*****
            if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) {
                ProgressBar progress = getCircularProgressBar(false);
                if (progress != null) {
                    progress.setIndeterminate(true);
                }
            }
    
            if ((features & (1 << FEATURE_SWIPE_TO_DISMISS)) != 0) {
                registerSwipeCallbacks();
            }
            return contentParent;
        }
    
    

    我们可以看到首先获得style属性,判断是否有WindowNoTitle等等一些属性,然后调用一大堆requestFeature()和setFlags(),就是对Window一些状态属性的设置,mContentParentExplicitlySet是一个标记,在调用setContentView()中就会设置为true,这就解开了文章开头的疑问,为什么要在setContentView()之前设置requestFeature(Window.FEATURE_NO_TITLE);

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

    上面其中还有一段代码 int features = getLocalFeatures(),来获得对Window所设置的features ,系统会根据features 来加载不同的XML文件,这个文件就是将要加载到window decor里面的,再次说明requestFeature()要在setContentView()之前设置,我们来看一下加载的其中一个XML文件R.layout.screen_simple,

    R.layout.screen_simple

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:orientation="vertical">
        <ViewStub android:id="@+id/action_mode_bar_stub"
                  android:inflatedId="@+id/action_mode_bar"
                  android:layout="@layout/action_mode_bar"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:theme="?attr/actionBarTheme" />
        <FrameLayout
             android:id="@android:id/content"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:foregroundInsidePadding="false"
             android:foregroundGravity="fill_horizontal|top"
             android:foreground="?android:attr/windowContentOverlay" />
    </LinearLayout>
    

    这就是DecorView加载的布局,注意FrameLayout的id,通过代码④找到这个id为ID_ANDROID_CONTENT的ViewGroup,可以发现ID_ANDROID_CONTENT就是FrameLayout。

        /**
         * The ID that the main layout in the XML layout file should have.
         */
        public static final int ID_ANDROID_CONTENT = com.android.internal.R.id.content;
    

    最后又返回contentParent,也就是说mContentParent 就是这个id为content的FrameLayout。我们看下面的视图


    Activity加载UI-类图关系和视图结构.png

    我们总结一下图片,每一个Activity对应一个Window,PhoneWindow是Window的子类,DecorView是PhoneWindow的内部类,加载Activity显示的视图,现在对窗口的视图有了清晰的了解。

    LayoutInflater 把xml添加到Decorview分析

    1.include 为什么不能xml资源布局的根节点?
    2.merge 为什么作为xml资源布局的根节点?

    我们来看下setContentView()中mLayoutInflater.inflate(layoutResID, mContentParent)的部分,LayoutInflater的inflater(layoutResID,mContentParent)方法

        public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
            return inflate(resource, root, root != null);
        }
    
        public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
            final Resources res = getContext().getResources();
            if (DEBUG) {
                Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("
                        + Integer.toHexString(resource) + ")");
            }
    
            final XmlResourceParser parser = res.getLayout(resource);
            try {
                return inflate(parser, root, attachToRoot);
            } finally {
                parser.close();
            }
        }
    
        /**
         * Inflate a new view hierarchy from the specified XML node. Throws
         * {@link InflateException} if there is an error.
         * <p>
         * <em><strong>Important</strong></em>   For performance
         * reasons, view inflation relies heavily on pre-processing of XML files
         * that is done at build time. Therefore, it is not currently possible to
         * use LayoutInflater with an XmlPullParser over a plain XML file at runtime.
         * 
         * @param parser XML dom node containing the description of the view
         *        hierarchy.
         * @param root Optional view to be the parent of the generated hierarchy (if
         *        <em>attachToRoot</em> is true), or else simply an object that
         *        provides a set of LayoutParams values for root of the returned
         *        hierarchy (if <em>attachToRoot</em> is false.)
         * @param attachToRoot Whether the inflated hierarchy should be attached to
         *        the root parameter? If false, root is only used to create the
         *        correct subclass of LayoutParams for the root view in the XML.
         * @return The root View of the inflated hierarchy. If root was supplied and
         *         attachToRoot is true, this is root; otherwise it is the root of
         *         the inflated XML file.
         */
        public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
            synchronized (mConstructorArgs) {
                Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");
    
                final Context inflaterContext = mContext;
                final AttributeSet attrs = Xml.asAttributeSet(parser);    //获得的是Activity显示布局的属性
                Context lastContext = (Context) mConstructorArgs[0];
                mConstructorArgs[0] = inflaterContext;
                View result = root;                                       //将DecorView布局中的FrameLayout赋值给result 
                 ...
                 // Look for the root node.                         //找到根节点
                    int type;
                    while ((type = parser.next()) != XmlPullParser.START_TAG &&
                            type != XmlPullParser.END_DOCUMENT) {
                        // Empty
                    }
                    if (TAG_MERGE.equals(name)) {
                        if (root == null || !attachToRoot) {
                            throw new InflateException("<merge /> can be used only with a valid "
                                    + "ViewGroup root and attachToRoot=true");
                        }
    
                        rInflate(parser, root, inflaterContext, attrs, false);
                    } else {
                         // Temp is the root view that was found in the xml
                        final View temp = createViewFromTag(root, name, inflaterContext, attrs);
                        ViewGroup.LayoutParams params = null;
                        if (root != null) {
                            // Create layout params that match root, if supplied   //创建与root匹配的布局参数(如果提供)
                            params = root.generateLayoutParams(attrs);
                            if (!attachToRoot) {
                                // Set the layout params for temp if we are not
                                // attaching. (If we are, we use addView, below)
                                temp.setLayoutParams(params);
                            }
                        }
                        // Inflate all children under temp against its context.          //填充temp下面的子View
                        rInflateChildren(parser, temp, attrs, true);      
                        ...
                    }
                    ...
                return result;
            }
        }
    

    上面三段代码依次调用,如果是meger标签时调用 rInflate(parser, root, inflaterContext, attrs, false);如果不是meger标签时会调用View temp = createViewFromTag(root, name, inflaterContext, attrs); 上面有一句注释// Temp is the root view that was found in the xml,意思是:Temp是在xml中找到的根视图,也就是我们Activity显示布局的根视图,然后调用root.generateLayoutParams(attrs);来解析根布局的参数并进行设置, 下面我们看下rInflateChildren(parser, temp, attrs, true)

        /**
         * Recursive method used to inflate internal (non-root) children. This
         * method calls through to {@link #rInflate} using the parent context as
         * the inflation context.
         * <strong>Note:</strong> Default visibility so the BridgeInflater can
         * call it.
         */
        final void rInflateChildren(XmlPullParser parser, View parent, AttributeSet attrs,
                boolean finishInflate) throws XmlPullParserException, IOException {
            rInflate(parser, parent, parent.getContext(), attrs, finishInflate);
        }
    

    我们发现最后都是调用rInflate()只是最后一个参数finishInflate不同,meger传false,反之。

        /**
         * Recursive method used to descend down the xml hierarchy and instantiate
         * views, instantiate their children, and then call onFinishInflate().
         * <p>
         * <strong>Note:</strong> Default visibility so the BridgeInflater can
         * override it.
         */
        void rInflate(XmlPullParser parser, View parent, Context context,
                AttributeSet attrs, boolean finishInflate) throws XmlPullParserException, IOException {
    
            final int depth = parser.getDepth();
            int type;
    
            while (((type = parser.next()) != XmlPullParser.END_TAG ||
                    parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
    
                if (type != XmlPullParser.START_TAG) {
                    continue;
                }
    
                final String name = parser.getName();
                
                if (TAG_REQUEST_FOCUS.equals(name)) {
                    parseRequestFocus(parser, parent);
                } else if (TAG_TAG.equals(name)) {
                    parseViewTag(parser, parent, attrs);
                } else if (TAG_INCLUDE.equals(name)) {
                    if (parser.getDepth() == 0) {
                        throw new InflateException("<include /> cannot be the root element");
                    }
                    parseInclude(parser, context, parent, attrs);
                } else if (TAG_MERGE.equals(name)) {
    ![LayoutInflater.png](http:https://img.haomeiwen.com/i4029647/7d295d44aaf5b9aa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
                    throw new InflateException("<merge /> must be the root element");
                } else {
                    final View view = createViewFromTag(parent, name, context, attrs);
                    final ViewGroup viewGroup = (ViewGroup) parent;
                    final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
                    rInflateChildren(parser, view, attrs, true);
                    viewGroup.addView(view, params);
                }
            }
    
            if (finishInflate) {
                parent.onFinishInflate();
            }
        }
    

    我们发现是一个while循环,也就是通过遍历去解析的,看下解析过程会判断节点的名称,其中判断到TAG_INCLUDE和TAG_MERGE标签时我们会看到抛出两个异常,这就解释了开始提出的两个问题。最后else还是重复了rInflateChildren(),然后添加到viewGroup里面,这个过程用下面图片说明一下解析XML 的大致过程。

    LayoutInflater.png

    现在我们用AS开发默认都是继承AppCompatActivity,那么AppCompatActivity的setContentView()也是这样呢!!答案肯定不是的,我们下篇分析!
    (第一篇源码分析文章,希望读者给出建议!)

    相关文章

      网友评论

        本文标题:Activity的setContentView()源码分析

        本文链接:https://www.haomeiwen.com/subject/uszrdxtx.html