Activity的setContentView源码

activity.setContentView()

public void setContentView(@LayoutRes int layoutResID) {
    // 调用PhoneWindow.setContentView();
    getWindow().setContentView(layoutResID);
    initWindowDecorActionBar();
}

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) {
        //生成DecorView,加载根布局。
        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 {
        //将layout添加到mContentParent
        mLayoutInflater.inflate(layoutResID, mContentParent);
    }
    mContentParent.requestApplyInsets();
    final Callback cb = getCallback();
    if (cb != null && !isDestroyed()) {
        cb.onContentChanged();
    }
    mContentParentExplicitlySet = true;
}

installDecor

private void installDecor() {
    mForceDecorInstall = false;
    if (mDecor == null) {
        //生成DecorView
        mDecor = generateDecor(-1);
    } else {
        mDecor.setWindow(this);
    }
    if (mContentParent == null) {
        //生成ContentView
    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().getResources());
            if (mTheme != -1) {
                context.setTheme(mTheme);
            }
        }
    } else {
        context = getContext();
    }
    return new DecorView(context, featureId, this, getAttributes());
}

generateLayout

protected ViewGroup generateLayout(DecorView decor) {

    ....

   else {
        // Embedded, so no decoration is needed.
  layoutResource = R.layout.screen_simple;
        // System.out.println("Simple!");
  }

    mDecor.startChanging();
    // 将根据不同的主题的根布局,加载DecorView中。
    mDecor.onResourcesLoaded(mLayoutInflater, layoutResource);
    //dahai:这个是加载SetContentView()的 layoutID;
    ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);

    mDecor.finishChanging();

    return contentParent;
}

Window.ID_ANDROID_CONTENT

public static final int ID_ANDROID_CONTENT = com.android.internal.R.id.content;
ViewGroup contentParent = (ViewGroup)findViewById(Window.ID_ANDROID_CONTENT);
ViewGroup contentParent = (ViewGroup)findViewById(android.R.id.content);

R.layout.screen_simple

R.layout.screen_simple

你可能感兴趣的:(Activity的setContentView源码)