正文
public Window getWindow() {
return mWindow;
}
mWindow是在哪初始化的呢
?继续定位,我们发现是在Activiy的Attach方法进行的初始化,它是一个PhoneWindow类。
final void attach(Context context, ActivityThread aThread,
Instrumentation instr, IBinder token, int ident,
Application application, Intent intent, ActivityInfo info,
CharSequence title, Activity parent, String id,
NonConfigurationInstances lastNonConfigurationInstances,
Configuration config, String referrer,
IVoiceInteractor voiceInteractor) {
attachBaseContext(context);
mWindow = new PhoneWindow(this);}
所以,这里调用的的PhoneWindow的setContentView方法。
public void setContentView(int layoutResID) {
if (mContentParent == null) {
installDecor();
} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
mContentParent.removeAllViews();
}
mLayoutInflater.inflate(layoutResID, mContentParent);
}
此处代码的意思是
1.如果mContentParent 为空的时候,这个时候要对DecorView进行初始化。
2.通过LayoutInflate服务将该ID对应的资源文件解析成view,并且添加到它的父view(mContentParent)中。
关于LayoutInflate服务将该ID对应的资源文件解析成view 这个过程(也就是mLayoutInflater.inflate(layoutResID, mContentParent);这个过程),可以关注我的后续博文(最近公司活比较多,没太多时间写)
再看一下initDecor()
private void installDecor() {
if (mDecor == null) {
mDecor = generateDecor();
}
if (mContentParent == null) {
mContentParent = generateLayout(mDecor);
}
在这里我们知道布局文件解析成的view必须添加到mContenParent这个view中,而而mContentParent是通过DecorView产生的,但是它们三者的关系是什么样的呢?
这里我们不从具体的代码分析了,分析过多容易晕,直接从应用显示的View视图进行分析。这里我们用了Hierarchy Viewer工具,
在层级图中,先找到我们自定义的布局,这是一个相对布局,其中包含了一个ImageView
继续向上,FrameLayout是什么?根据上文的理解,它直接包含了我们自定义的布局文件,应该就是mCotentParent这个view。
那和它并列的布局是什么呢?