源码中单例模式工具类
public abstract class Singleton<T> {@UnsupportedAppUsagepublic Singleton() {}@UnsupportedAppUsageprivate T mInstance;protected abstract T create();@UnsupportedAppUsagepublic final T get() {synchronized (this) {if (mInstance == null) {mInstance = create();}return mInstance;}}}
LayoutInflater
LayoutInflater是abstract,实现类是PhoneLayoutInflater。
Activity -> PhoneWindow -> DecorView -> DefaultLayout -> ViewGroup: mContentParent -> 用户xml布局。

LayoutInflater的核心是inflate方法,通过inflate方法把布局添加到mContentParent中,mContentParent就是上层的DecorView,也就是把布局添加到DecorView中,DecorView继承自FrameLayout,其实就是个ViewGroup。在Activity中调用onResume后,会将onCreate中的sestContentView布局加载到DecorView中,DecorView添加到WindowManager中,最终现实用户布局到手机屏幕上。
inflate()
inflate方法内部的重要方法:createViewFromTag,rInflate。
inflate方法做了哪些事情?
- 解析xml中的根标签;
- 如果根标签是merge,调用rInflate解析,然后将merge标签下的所有子View直接添加到根标签;
- 如果是普通xml元素,用createViewFromTag对元素解析;
- 调用rInflate解析temp根元素下所有子View,并且将这些子View添加到temp中;
- 返回解析到根视图。
createViewFromTag()
onCreateView:解析内置View,PhoneLayoutInflater复写了onCreateView。
createView:解析自定义View
rInflate()
通过DFS构造视图树,解析View时候就会递归调用rInflate方法,最后通过回溯把没个View添加到parent中。通过DFS和回溯完成整个视图树的构建。
WindowManagerGlobal
WindowManagerGlobal是个单例模式,同时也属于桥梁模式。WindowManagerGlobal是WindowManager与WindowManagerImpl的桥梁。
思考
为什么Activity调用onResume后才会现实布局?
