之前有一篇文章源码分析了Activity#setContentView,但是目前我们写的Activity基本都是继承自AppCompactActivity,google也是建议继承AppCompactActivity可以提高一些兼容性,今天我们就来一探AppCompactActivity#setContentView的究竟,看看跟Activity#setContentView有何异同。
依照之前的惯例,先来看一张流程图,对整个流程有个大概的了解,便于理解后面的源码分析
好,我们从熟悉的MainActivity开始
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
跟进setContentView(R.layout.activity_main)
public void setContentView(@LayoutRes int layoutResID) {
this.getDelegate().setContentView(layoutResID);
}
getDelegate()返回的是AppCompatDelegate类型,AppCompatDelegate是一个接口,我们找到其实现类AppCompatDelegateImpl#setContentView
public void setContentView(int resId) {
this.ensureSubDecor();
ViewGroup contentParent = (ViewGroup)this.mSubDecor.findViewById(16908290);
contentParent.removeAllViews();
LayoutInflater.from(this.mContext).inflate(resId, contentParent);
this.mOriginalWindowCallback.onContentChanged();
}
这里ensureSubDecor()主要就是确保创建出SubDecor并给mSubDecor赋值,mSubDecor是一个ViewGroup,确保下面调用(ViewGroup)this.mSubDecor.findViewById(16908290)得到contentParent不会抛出异常,接着删除contentParent所有子View,然后将我们传入的resId渲染到contentParent上,好,我们继续跟进AppCompatDelegateImpl#ensureSubDecor
private void ensureSubDecor() {
if (!this.mSubDecorInstalled) {
// 看这里
this.mSubDecor = this.createSubDecor();
CharSequence title = this.getTitle();
if (!TextUtils.isEmpty(title)) {
if (this.mDecorContentParent != null) {
this.mDecorContentParent.setWindowTitle(title);
} else if (this.peekSupportActionBar() != null) {
this.peekSupportActionBar().setWindowTitle(title);
} else if (this.mTitleView != null) {
this.mTitleView.setText(title);
}
}
this.applyFixedSizeWindow();
this.onSubDecorInstalled(this.mSubDecor);
this.mSubDecorInstalled = true;
AppCompatDelegateImpl.PanelFeatureState st = this.getPanelState(0, false);
if (!this.mIsDestroyed && (st == null || st.menu == null)) {
this.invalidatePanelMenu(108);
}
}
}
这里面主要的操作就是调用createSubDecor()来创建SubDecor并将返回值赋值给mSubDecor,继续跟进AppCompatDelegateImpl#createSubDecor
private ViewGroup createSubDecor() {
TypedArray a = this.mContext.obtainStyledAttributes(styleable.AppCompatTheme);
if (!a.hasValue(styleable.AppCompatTheme_windowActionBar)) {
a.recycle();
throw new IllegalStateException("You need to use a Theme.AppCompat theme (or descendant) with this activity.");
} else {
if (a.getBoolean(styleable.AppCompatTheme_windowNoTitle, false)) {
this.requestWindowFeature(1);
} else if (a.getBoolean(styleable.AppCompatTheme_windowActionBar, false)) {
this.requestWindowFeature(108);
}
if (a.getBoolean(styleable.AppCompatTheme_windowActionBarOverlay, false)) {
this.requestWindowFeature(109);
}
if (a.getBoolean(styleable.AppCompatTheme_windowActionModeOverlay, false)) {
this.requestWindowFeature(10);
}
this.mIsFloating = a.getBoolean(styleable.AppCompatTheme_android_windowIsFloating, false);
a.recycle();
// 注释1
this.mWindow.getDecorView();
LayoutInflater inflater = LayoutInflater.from(this.mContext);
ViewGroup subDecor = null;
// 注释2
if (!this.mWindowNoTitle) {
if (this.mIsFloating) {
subDecor = (ViewGroup)inflater.inflate(layout.abc_dialog_title_material, (ViewGroup)null);
this.mHasActionBar = this.mOverlayActionBar = false;
} else if (this.mHasActionBar) {
TypedValue outValue = new TypedValue();
this.mContext.getTheme().resolveAttribute(attr.actionBarTheme, outValue, true);
Object themedContext;
if (outValue.resourceId != 0) {
themedContext = new ContextThemeWrapper(this.mContext, outValue.resourceId);
} else {
themedContext = this.mContext;
}
subDecor = (ViewGroup)LayoutInflater.from((Context)themedContext).inflate(layout.abc_screen_toolbar, (ViewGroup)null);
this.mDecorContentParent = (DecorContentParent)subDecor.findViewById(id.decor_content_parent);
this.mDecorContentParent.setWindowCallback(this.getWindowCallback());
if (this.mOverlayActionBar) {
this.mDecorContentParent.initFeature(109);
}
if (this.mFeatureProgress) {
this.mDecorContentParent.initFeature(2);
}
if (this.mFeatureIndeterminateProgress) {
this.mDecorContentParent.initFeature(5);
}
}
} else {
if (this.mOverlayActionMode) {
subDecor = (ViewGroup)inflater.inflate(layout.abc_screen_simple_overlay_action_mode, (ViewGroup)null);
} else {
subDecor = (ViewGroup)inflater.inflate(layout.abc_screen_simple, (ViewGroup)null);
}
if (VERSION.SDK_INT >= 21) {
ViewCompat.setOnApplyWindowInsetsListener(subDecor, new OnApplyWindowInsetsListener() {
public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) {
int top = insets.getSystemWindowInsetTop();
int newTop = AppCompatDelegateImpl.this.updateStatusGuard(top);
if (top != newTop) {
insets = insets.replaceSystemWindowInsets(insets.getSystemWindowInsetLeft(), newTop, insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom());
}
return ViewCompat.onApplyWindowInsets(v, insets);
}
});
} else {
((FitWindowsViewGroup)subDecor).setOnFitSystemWindowsListener(new OnFitSystemWindowsListener() {
public void onFitSystemWindows(Rect insets) {
insets.top = AppCompatDelegateImpl.this.updateStatusGuard(insets.top);
}
});
}
}
if (subDecor == null) {
throw new IllegalArgumentException("AppCompat does not support the current theme features: { windowActionBar: " + this.mHasActionBar + ", windowActionBarOverlay: " + this.mOverlayActionBar + ", android:windowIsFloating: " + this.mIsFloating + ", windowActionModeOverlay: " + this.mOverlayActionMode + ", windowNoTitle: " + this.mWindowNoTitle + " }");
} else {
if (this.mDecorContentParent == null) {
this.mTitleView = (TextView)subDecor.findViewById(id.title);
}
ViewUtils.makeOptionalFitsSystemWindows(subDecor);
// 注释3
ContentFrameLayout contentView = (ContentFrameLayout)subDecor.findViewById(id.action_bar_activity_content);
ViewGroup windowContentView = (ViewGroup)this.mWindow.findViewById(16908290);
if (windowContentView != null) {
while(windowContentView.getChildCount() > 0) {
View child = windowContentView.getChildAt(0);
windowContentView.removeViewAt(0);
contentView.addView(child);
}
// 注释4
windowContentView.setId(-1);
contentView.setId(16908290);
if (windowContentView instanceof FrameLayout) {
((FrameLayout)windowContentView).setForeground((Drawable)null);
}
}
this.mWindow.setContentView(subDecor);
contentView.setAttachListener(new OnAttachListener() {
public void onAttachedFromWindow() {
}
public void onDetachedFromWindow() {
AppCompatDelegateImpl.this.dismissPopups();
}
});
return subDecor;
}
}
}
我们看到注释1处,调用了mWindow.getDecorView(),这里Window毫无疑问是Window的唯一实现类PhoneWindow,我们先看看PhoneWindow#getDecorView
@Override
public final View getDecorView() {
if (mDecor == null || mForceDecorInstall) {
installDecor();
}
return mDecor;
}
这里面调用了installDecor(),我们之前一篇文章分析Activity#setContentView源码的时候就分析了installDecor(),这里不在赘述,详情可以看从源码角度分析Activity#setContentView,主要做的工作就是初始化mDecor和mContentParent。
好,我们继续回到AppCompatDelegateImpl#createSubDecor,看到注释2处,这里主要是根据不同的主题特性渲染出不同的布局来初始化subDecor。接着看注释3处,通过subDecor.findViewById(id.action_bar_activity_content)得到contentView,通过mWindow.findViewById(16908290)得到windowContentView,咦,这个16908290似曾相识,在那个月黑风高的夜晚……,没错,就是在AppCompatDelegateImpl#setContentView我们通过mSubDecor.findViewById(16908290)得到contentParent作为承载我们自己的布局的父容器,但其实contentView就是在subDecor中的我们要找的父容器,这从这里看来,我们在AppCompatDelegateImpl#setContentView得到的contentParent似乎是windowContentView,跟subDecor并没有关联上,那AppCompatDelegateImpl#setContentView中调用mSubDecor.findViewById(16908290)不是会抛出异常吗?别着急,我们继续往下看,看注释4处,把windowContentView的id设置为-1,接着将contentView的id设置为16908290,这招偷天换日使得在AppCompatDelegateImpl#setContentView我们通过mSubDecor.findViewById(16908290)得到contentParent就是subDecor.findViewById(id.action_bar_activity_content)得到contentView,这下应该就很明了了。然后调用this.mWindow.setContentView(subDecor)将subDecor添加到PhoneWindow的mContentParent中,最后返回了subDecor。
最后总结下AppCompactActivity#setContentView与Activity#setContentView的区别:
当MainActivity继承自AppCompactActivity时,会根据不同的主题特性在contentParent容器里面添加一个不同主题的subDecor容器,在subDecor容器里面有一个id为action_bar_activity_content的ContentFrameLayout容器,并将我们的布局渲染到ContentFrameLayout里面
当MainActivity继承自Activity时,直接将我们的布局渲染到contentParent容器里面
继承AppCompatActivity的Activity视图结构如下