View是如何被加载到屏幕窗口的(看源码的正确姿势)

一入源码深似海,半天再也出来

从超级熟悉的入口开始

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

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

发现调用了getWindow()的setContentView(), 进去看下getWindow()

    /**
     * Retrieve the current {@link android.view.Window} for the activity.
     * This can be used to directly access parts of the Window API that
     * are not available through Activity/Screen.
     *
     * @return Window The current window, or null if the activity is not
     *         visual.
     */
    public Window getWindow() {
        return mWindow;
    }

直接返回了mWindow,点mWindow

private Window mWindow

发现是个Window对象,点Window看看是什么玩意

 * 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 {
  ...
}

代码还没看,直接在注释里发现The only existing implementation of this abstract class is android.view.PhoneWindow。
那直接打开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();
        } 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);
        }
        ...
    }

发现有个叫Decor的玩意正在初始化,直接点进去:

 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);
          ...
        }
        ...
}

进来又发现两个玩意在初始化:
mDecor = generateDecor(-1);
mContentParent = 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());
                if (mTheme != -1) {
                    context.setTheme(mTheme);
                }
            }
        } else {
            context = getContext();
        }
        return new DecorView(context, featureId, this, getAttributes());
    }

发现return了一个DecorView,点进去看看是个什么东西

public class DecorView extends FrameLayout

发现就是一个FrameLayout的子类,也就是一个容器,
然后回到generateLayout(mDecor),这个方法把初始化好的一个DecorView传了进去,点进去:

   protected ViewGroup generateLayout(DecorView decor) {
      //这里贼长... 不贴了,想看的自己去看看,就是设置样式主题什么的
      ...
      ...
      // Inflate the window decor.

        int layoutResource;
        int features = getLocalFeatures();
        if ((features & (1 << FEATURE_SWIPE_TO_DISMISS)) != 0) {
            layoutResource = R.layout.screen_swipe_dismiss;
            setCloseOnSwipeEnabled(true);
        } else if (...){
          ...
          //再省略一段,这里是根据不同的features给layoutResource赋值。
        }
        mDecor.onResourcesLoaded(mLayoutInflater, layoutResource);
        ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
        ...
        return contentParent;
   }

这里有两个关键的点:

  • 发现DecorView调用了onResourcesLoaded(mLayoutInflater, layoutResource)这么个方法,接收了mLayoutInflater和layoutResource
  • 初始化一个ViewGroup: ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);并且返回了这个ViewGroup

先进去onResourcesLoaded看看做了什么:

   void onResourcesLoaded(LayoutInflater inflater, int layoutResource) {
        if (mBackdropFrameRenderer != null) {
            loadBackgroundDrawablesIfNeeded();
            mBackdropFrameRenderer.onResourcesLoaded(
                    this, mResizingBackgroundDrawable, mCaptionBackgroundDrawable,
                    mUserCaptionBackgroundDrawable, getCurrentColor(mStatusColorViewState),
                    getCurrentColor(mNavigationColorViewState));
        }

        mDecorCaptionView = createDecorCaptionView(inflater);
        final View root = inflater.inflate(layoutResource, null);
        if (mDecorCaptionView != null) {
            if (mDecorCaptionView.getParent() == null) {
                addView(mDecorCaptionView,
                        new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
            }
            mDecorCaptionView.addView(root,
                    new ViewGroup.MarginLayoutParams(MATCH_PARENT, MATCH_PARENT));
        } else {

            // Put it below the color views.
            addView(root, 0, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
        }
        mContentRoot = (ViewGroup) root;
        initializeElevation();
    }

阅读DecorView的这个函数,发现把layoutResource解析并调用addView添加到了DecorView,也就是上面的实例,mDecor中。
然后回去看看findViewById(ID_ANDROID_CONTENT), 点击这个ID_ANDROID_CONTENT看看是什么:

    /**
     * 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;

这里说到这个资源id是主容器中的id,而且一定存在的。
总结一下generateLayout做了什么

  • 根据不同主题或者特性设置了不同的layoutResource(这些layoutResource都是系统布局,查看这些布局会发现里面容器里包含一个ActionBar的title,一个id为com.android.internal.R.id.content的FrameLayout容器)
  • 把layoutResource解析到mDecor(DecorView)中
  • 得到id固定为com.android.internal.R.id.content的ViewGroup并返回

看完installDecor()中主要的函数generateDecor() 和generateLayout() 到这里分析完了。然后再回到上面调用installDecor()的地方,上面贴过了,就是在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();
        } 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);
        }
        ...
    }

看这里的这一句 mLayoutInflater.inflate(layoutResID, mContentParent),mContentParent就是刚才通过generateLayout得到的id为com.android.internal.R.id.content的容器,layoutResID是在Activity里设置的资源id,到此设置完成,在总结一下:

  • Activity中调用setContentView(layoutResID)其实是调用了Window的setContentView(layoutResID),而Window的唯一实现类是PhoneWindow。所以调用的是PhoneWindow的setContentView(layoutResID)
  • PhoneWindow的setContentView()里调用了两个方法:installDecor()和mLayoutInflater.inflate(layoutResID, mContentParent)。installDecor是为了得到容器mContentParent, 然后mLayoutInflater.inflate把资源解析上去
  • installDecor()为了得到容器mContentParent主要调用了两个方法:mDecor = generateDecor(-1)和 mContentParent = generateLayout(mDecor)。 generateDecor是为了实例化一个DecorView,generateLayout接收该DecorView的实例,然后根据不同主题、样式设置不同的layoutResource,并返回一个固定id为com.android.internal.R.id.content的FrameLayout(这些不同的系统layoutResource中都会有这么一个FrameLayout),返回的这个FrameLayout。就是上一步中我们想要得到的mContentParent,也就是接收在Activity中通过setContentView 设置的layoutResID的容器。

最后一张图:

《View是如何被加载到屏幕窗口的(看源码的正确姿势)》 view层级.jpeg

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