一.分析层层调用的关键代码
-
设置布局文件:每个Activity的onCreate()中 setContentView(R.layout.xx)。
-
实际是调用Window抽象类的方法:this.getWindow().setContentView(R.layout.xx)。
-
Window唯一实现类PhoneWindow实现的setContentView():
关键调用方法:
1)installDecor()
1> 创建布局容器 : 先创建顶层View DecorView, 通过 mDecor 创建 ViewGroup布局容器。
2> 加载基础布局 : 调用 generateLayout(), 处理 requestFeature 设置, 根据处理结果加载不同的基础布局;
默认加载布局R.layout.screen_simple,垂直方向线性布局,包含状态栏ViewStub+Activity传入的自定义布局存放在FrameLayout。
2)mLayoutInflater.inflate() 加载布局文件
加载开发者自己的布局文件,LayoutInfater是PhoneWindow创建时初始化的布局加载器。
布局加载器加载过程:进行xml解析,解析出具体的组件和层级。
public class PhoneWindow extends Window implements Callback {
//窗口最顶层的 View
private DecorView mDecor;
//窗口的内容放置器
ViewGroup mContentParent;
//PhoneWindow 创建时初始化 mLayoutInflater 布局加载器成员变量
private LayoutInflater mLayoutInflater;
public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
public void setContentView(int layoutResID) {
//对feature 属性进行初始化生效操作
if (mContentParent == null) {
//1.关键方法:installDecor()
installDecor();
} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
mContentParent.removeAllViews();
}
if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {