Android setContentView源码阅读
作者:互联网
阅读源码查看系统如何加载布局
Acticity setContentView源码阅读
public void setContentView(@LayoutRes int layoutResID) {
getWindow().setContentView(layoutResID);
initWindowDecorActionBar();
}
点击去发现是个抽象类
public abstract void setContentView(@LayoutRes int layoutResID);
1、通过getWindow()找到具体的实现类PhoneWindow
mWindow = new PhoneWindow(this, window);
2、查看PhoneWindow如何具体加载布局 继续看代码
@Override
public void setContentView(int layoutResID) {
if (mContentParent == null) {
installDecor();
}
mLayoutInflater.inflate(layoutResID, mContentParent);
3、查看installDecor(); 里面代码 返回一个DecorView,DecorView继承FrameLayout
//赋值给了Phonewindow成员变量 mDecor
protected DecorView generateDecor(int featureId) {
...
return new DecorView(context, featureId, this, getAttributes());
}
4、继续看里面installDecor又干了什么事情
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);
。。。
protected ViewGroup generateLayout(DecorView decor) {
// Apply data from current theme.
// 构建布局主要代码
mDecor.startChanging();
mDecor.onResourcesLoaded(mLayoutInflater, layoutResource);
ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
// 构建系统布局
``
标签:ViewGroup,setContentView,void,layoutResID,DecorView,源码,Android,mDecor 来源: https://blog.csdn.net/fc289738973/article/details/120418145