Android中setContentView(int laoutId) 干了些什么事

1、Activity中的setContentView

    public void setContentView(@LayoutRes int layoutResID) {
        // 交给PhoneWindow处理
        getWindow().setContentView(layoutResID);
        initWindowDecorActionBar();
    }

2、PhoneWindow中的setContentView

    @Override
    public void setContentView(int layoutResID) {

        if (mContentParent == null) {
            // 初始化DecorView(顶级view)
            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 {
            // 将自定义的布局放入到mContentParent中
            mLayoutInflater.inflate(layoutResID, mContentParent);
        }
        mContentParent.requestApplyInsets();
        final Callback cb = getCallback();
        if (cb != null && !isDestroyed()) {
            // 通知内容改变,该方法可以由Activity类实现
            cb.onContentChanged();
        }
        mContentParentExplicitlySet = true;
    }

3、再看installDecor

    private void installDecor() {
        mForceDecorInstall = false;
        if (mDecor == null) {
            // 初始化DecorView mDecor:DecorView
            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);

你可能感兴趣的:(Android)