DecorVIew 的创建
我们通过一个示例来看看顶层View(DecorView)是怎么创建出来的。
示例:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
在上面我们一般都会调用setContentView方法来设置布局。该方法调用了Activity的setContentView方法,我们去看看:
public class Activity extends ContextThemeWrapper
implements LayoutInflater.Factory2,
Window.Callback, KeyEvent.Callback,
OnCreateContextMenuListener, ComponentCallbacks2,
Window.OnWindowDismissedCallback {
.......
public void setContentView(int layoutResID) {
getWindow().setContentView(layoutResID);//调用与之关联Window中的setContentView()方法
initWindowDecorActionBar();
}
......
}
在父类Activity中其实就是调用与之关联的Window(真正的对象类型是PhoneWindow)setContentView()来创建DecorView对象。进入源码看看实现的过程:
进入Window类(实现对象类型是PhoneWindow)的setContentView():
public class PhoneWindow extends Window implements MenuBuilder.Callback {
......
public void setContentView(int layoutResID) {
if (mContentParent == null) {
installDecor();
} else {
mContentParent.removeAllViews();
}
mLayoutInflater.inflate(layoutResID, mContentParent);