setContentView()可以說是大家最熟悉的方法了,Activity中設置xml布局時調用。下面看看源碼實現。
AppCompatActivity.setContentView()
public void setContentView(@LayoutRes int layoutResID) {
getDelegate().setContentView(layoutResID);
}
public AppCompatDelegate getDelegate() {
if (mDelegate == null) {
mDelegate = AppCompatDelegate.create(this, this);
}
return mDelegate;
}
AppCompatDelegate是個抽象類
AppCompatDelegate.create()
public static AppCompatDelegate create(Activity activity, AppCompatCallback callback) {
return new AppCompatDelegateImpl(activity, activity.getWindow(), callback);
}
實現類是AppCompatDelegateImpl,構造方法
AppCompatDelegateImpl(Context context, Window window, AppCompatCallback callback) {
mContext = context;
mWindow = window;
mAppCompatCallback = callback;
mOriginalWindowCallback = mWindow.getCallback();
if (mOriginalWindowCallback instanceof AppCompatWindowCallback) {
throw new IllegalStateException(
"AppCompat has already installed itself into the Window");
}
mAppCompatWindowCallback = new AppCompatWindowCallback(mOriginalWindowCallback);
//設置回調
mWindow.setCallback(mAppCompatWindowCallback);
//Window背景色
final TintTypedArray a = TintTypedArray.obtainStyledAttributes(
context, null, sWindowBackgroundStyleable);
final Drawable winBg = a.getDrawableIfKnown(0);
if (winBg != null) {
mWindow.setBackgroundDrawable(winBg);
}
a.recycle();
}
setContentView()最終調用到了代理類AppCompatDelegateImpl.setContentView()
public void setContentView(int resId) {
//創建SubDecor
ensureSubDecor();
//id為content的FrameLayout
ViewGroup contentParent = (ViewGroup) mSubDecor.findViewById(android.R.id.content);
//移除content上所有view
contentParent.removeAllViews();
//加載xml布局到content
LayoutInflater.from(mContext).inflate(resId, contentParent);
//回調
mOriginalWindowCallback.onContentChanged();
}
AppCompatDelegateImpl.ensureSubDecor()
private void ensureSubDecor() {
if (!mSubDecorInstalled) {
//創建SubDecor
mSubDecor = createSubDecor();
//ActionBar title
CharSequence title = getTitle();
if (!TextUtils.isEmpty(title)) {
if (mDecorContentParent != null) {
mDecorContentParent.setWindowTitle(title);
} else if (peekSupportActionBar() != null) {
peekSupportActionBar().setWindowTitle(title);
} else if (mTitleView != null) {
mTitleView.setText(title);
}
}
applyFixedSizeWindow();
onSubDecorInstalled(mSubDecor);
mSubDecorInstalled = true;
//menu
PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, false);
if (!mIsDestroyed && (st == null || st.menu == null)) {
invalidatePanelMenu(FEATURE_SUPPORT_ACTION_BAR);
}
}
}
AppCompatDelegateImpl.createSubDecor()
...
final LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup subDecor = null;
//有無title
if (!mWindowNoTitle) {
//Activity彈框形式出現
if (mIsFloating) {
subDecor = (ViewGroup) inflater.inflate(
R.layout.abc_dialog_title_material, null);
}else if (mHasActionBar) {//有ActionBar
subDecor = (ViewGroup) LayoutInflater.from(themedContext)
.inflate(R.layout.abc_screen_toolbar, null);
mDecorContentParent = (DecorContentParent) subDecor
.findViewById(R.id.decor_content_parent);
}
} else {
if (mOverlayActionMode) {//覆蓋模式
subDecor = (ViewGroup) inflater.inflate(
R.layout.abc_screen_simple_overlay_action_mode, null);
} else {
subDecor = (ViewGroup) inflater.inflate(R.layout.abc_screen_simple, null);
}
}
final ContentFrameLayout contentView = (ContentFrameLayout) subDecor.findViewById(
R.id.action_bar_activity_content);
final ViewGroup windowContentView = (ViewGroup) mWindow.findViewById(android.R.id.content);
if (windowContentView != null) {
while (windowContentView.getChildCount() > 0) {
final View child = windowContentView.getChildAt(0);
windowContentView.removeViewAt(0);
contentView.addView(child);
}
//設置id
windowContentView.setId(View.NO_ID);
contentView.setId(android.R.id.content);
if (windowContentView instanceof FrameLayout) {
((FrameLayout) windowContentView).setForeground(null);
}
}
//SubDecor添加到window的DecorView
mWindow.setContentView(subDecor);
return subDecor;
mWindow.setContentView(subDecor)。window在Activity中初始化,實現類是PhoneWindow。
Activity.attach()
final void attach(...){
mWindow = new PhoneWindow(this, window, activityConfigCallback);
}
PhoneWindow.setContentView()
public void setContentView(View view) {
setContentView(view, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
}
public void setContentView(View view, ViewGroup.LayoutParams params) {
......
mContentParent.addView(view, params);
}
createSubDecor()方法根據不同的配置創建SubDecor,并調用PhoneWindow.setContentView()添加到window的DecorView。
總結:setContentView()調用到代理類AppCompatDelegateImpl.setContentView()方法,先創建SubDecor添加到window的DecorView,然后通過LayoutInflater將xml布局解析成View樹,添加到id為content的SubDecor子view中。