具体调用的是App兼容性代理类中方法,即AppConpatDelegateImpl.setContentView(layoutResID)。 其步骤如下: 1.准备好盛放layoutResID布局的View容器(mSubDecor) 2.从View容器中找到layoutResID的直接父容器(contentParent):mSubDecor.findViewById(android.R.id.content) 3.清空父容器 4.把layoutResID添加到父容器中
完整代码: @Override public void setContentView(int resId){ ensureSubDecor(); ViewGroup contentParent = mSubDecor.findViewById(android.R.id.content); contentParent.removeAllViews(); LayoutInflater.from(mContent).inflate(resId, contentParent); mAppCompatWindowCallback.getWrapped().onContentChanged(); }
由代码可知,主要工作在ensureSubDecor()中,其方法内容如下:
ensureSubDecor()的核心内容在于创建SubDecor,即createSubDecor()。其余部分处理title、fixedSizeWindow、subDecor创建的回调、菜单。
接下来看createSubDecor()怎么创建的。 1.通过context取得styleAttributes,并根据attribute设置title、actionBar 2.通过ensureWindow()准备好PhoneWidow,由PhoneWindow生产及管理最顶层View(DecorView)。 DecorView继承自FrameLayout。