Activity视图层级图
Activity的初始化
Activity的初始化是从从Activity的setContentView(),源码为
public void setContentView(View view) {
getWindow().setContentView(view);
initWindowDecorActionBar();
}
public void setContentView(@LayoutRes int layoutResID) {
getWindow().setContentView(layoutResID);
initWindowDecorActionBar();
}
public void setContentView(View view, ViewGroup.LayoutParams params) {
getWindow().setContentView(view, params);
initWindowDecorActionBar();
}
三种重载方法,都有调用initWindowDecorActionBar()为显示Decor和ActionBar
private void initWindowDecorActionBar() {
//window是抽象类,所以找其实现类phoneWindow
Window window = getWindow();
//初始化decorview
window.getDecorView();
if (isChild() || !window.hasFeature(Window.FEATURE_ACTION_BAR) || mActionBar != null) {
return;
}
//新建actionbar
mActionBar = new WindowDecorActionBar(this);
mActionBar.setDefaultDisplayHomeAsUpEnabled(mEnableDefaultActionBarUp);
//设置actionbar的图标
mWindow.setDefaultIcon(mActivityInfo.getIconResource());
mWindow.setDefaultLogo(mActivityInfo.getLogoResource());
}
PhoneWindow的实现类
public void setContentView(int layoutResID) {
// Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
// decor, when theme attributes and the like are crystalized. Do not check the feature
// before this happens.
if (mContentParent == null) {
//初始化decor
installDecor();
} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
mContentParent.removeAllViews();
}
if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID,
getContext());
transitionTo(newScene);
} else {
mLayoutInflater.inflate(layoutResID, mContentParent);
}
mContentParent.requestApplyInsets();
final Callback cb = getCallback();
if (cb != null && !isDestroyed()) {
cb.onContentChanged();
}
}
初始化DecorView代码
private void installDecor() {
if (mDecor == null) {
mDecor = generateDecor();
mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
mDecor.setIsRootNamespace(true);
if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
}
}
if (mContentParent == null) {
mContentParent = generateLayout(mDecor);
// Set up decor part of UI to ignore fitsSystemWindows if appropriate.
mDecor.makeOptionalFitsSystemWindows();
final DecorContentParent decorContentParent = (DecorContentParent) mDecor.findViewById(
R.id.decor_content_parent);
if (decorContentParent != null) {
mDecorContentParent = decorContentParent;
mDecorContentParent.setWindowCallback(getCallback());
if (mDecorContentParent.getTitle() == null) {
mDecorContentParent.setWindowTitle(mTitle);
}
再做一次反向操作,从DecorView中Remove。一步一步来看看取消了什么。
getDecorView(activity)..removeViewAt(decor.getChildCount() - 1)
public void removeViewAt(int index) {
//查看这个方法
removeViewInternal(index, getChildAt(index));
requestLayout();
invalidate(true);
}
public View getChildAt(int index) {
if (index < 0 || index >= mChildrenCount) {
return null;
}
return mChildren[index];
}
Ctrl+U 分别计算mChildren[0],mChildren[1],mChildren[2]的getleft,getright,gettop,getbottom的值。
mChildren[2]是最上方的状态条的视图。
mChildren[1]是最底部的导航条的视图。
mChildren[0]是背景整个视图。
注:国产手机不一定能看到。
参考文章:http://blog.csdn.net/mr_liabill/article/details/49534851
可通过对Activity视图层级的研究,将浮窗灵活运用。
http://www.jianshu.com/p/b4c23dee9206
http://www.jianshu.com/p/50fbec2baeb4