Android UI绘制流程及原理 一
我们分析的入口:
setContentView(R.layout.activity_main);
点击进入setContentView
public void setContentView(@LayoutRes int layoutResID) { getWindow().setContentView(layoutResID); initWindowDecorActionBar(); }
点击进入getWindow
(因为调用的是getWindow().setContentView)public Window getWindow() { return mWindow; }
点击进入Window查看到
可以看到这是一个抽象类,从注释中可以看到PhoneWindow是Window的实现类。
进入PhoneWindow
找到setContentView这个方法
查看installDecor()方法
if (mDecor == null) { mDecor = generateDecor(-1);
generateDecor(-1)方法得到new DecorView(context, featureId, this, getAttributes());
查看DecorView
public class DecorView extends FrameLayout implements RootViewSurfaceTaker, WindowCallbacks
DecorView继承于FrameLayout
回到上面的PhoneWindow的installDecor方法
mContentParent = generateLayout(mDecor);
找到该方法点击进入generateLayout方法,这个方法里设置了很多根据主题什么设置了一堆熟悉过
核心代码如下
这块代码是把不同主题布局添加到DecorViewmDecor.onResourcesLoaded(mLayoutInflater, layoutResource);
onResourcesLoaded方法详情
下面这块代码是根据主布局Id来获取主布局ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT);
contentParent就是我们要设置我们自己布局的父布局了。
代码展示如下 再回到PhoneWindow的setContentView方法
综上所述:View是如何添加到屏幕窗口上
- 创建顶层布局容器DecorView
- 在顶层布局中加载基础布局ViewGroup
- 将ContentView添加到基础布局中的FrameLayout中