Activity的源码

首先从setContentView这里调用的mWindow的 setConetView()

private Window mWindow;
public void setContentView(View view) {
getWindow().setContentView(view);
initWindowDecorActionBar();
} public Window getWindow() {
return mWindow;
}

那么这mWindow什么时候初始化?

final void attach(...) {
attachBaseContext(context);
mFragments.attachHost(null /*parent*/);
mWindow = new PhoneWindow(this, window, activityConfigCallback);
mWindow.setWindowControllerCallback(this);
mWindow.setCallback(this);
mWindow.setOnWindowDismissedCallback(this);
mWindow.getLayoutInflater().setPrivateFactory(this);
if (info.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED) {
mWindow.setSoftInputMode(info.softInputMode);
}
if (info.uiOptions != 0) {
mWindow.setUiOptions(info.uiOptions);
}
//...
//给window设置windowManger
mWindow.setWindowManager(
(WindowManager)context.getSystemService(Context.WINDOW_SERVICE),
mToken, mComponent.flattenToString(),
(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
if (mParent != null) {
mWindow.setContainer(mParent.getWindow());
}
mWindowManager = mWindow.getWindowManager();
mCurrentConfig = config; mWindow.setColorMode(info.colorMode);
}

可以看到这个mWindow 其实是一个PhoneWindow的实例,那么phoneWindow干了什么

可以查看PhoneWindow的源码

152    // This is the view in which the window contents are placed. It is either
153 // mDecor itself, or a child of mDecor where the contents go.
154 private ViewGroup mContentParent; 390 @Override
391 public void setContentView(View view, ViewGroup.LayoutParams params) {
392 // Note: FEATURE_CONTENT_TRANSITIONS may be set in the process of installing the window
393 // decor, when theme attributes and the like are crystalized. Do not check the feature
394 // before this happens.
395 if (mContentParent == null) {
396 installDecor();
397 } else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
398 mContentParent.removeAllViews();
399 }
400
401 if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
402 view.setLayoutParams(params);
403 final Scene newScene = new Scene(mContentParent, view);
404 transitionTo(newScene);
405 } else {
406 mContentParent.addView(view, params);
407 }
408 final Callback cb = getCallback();
409 if (cb != null && !isDestroyed()) {
410 cb.onContentChanged();
411 }
412 }

这里setContentView 主要是 先判断mContentParent是否初始化如果没有初始化调用installDecor 。然后将view添加到mContentParent的ViewGroup中

那么  installDecor  做了些什么

        private DecorView mDecor;
3551 private void installDecor() {
3552 if (mDecor == null) {
3553 mDecor = generateDecor();
3554 mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
3555 mDecor.setIsRootNamespace(true);
3556 if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
3557 mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
3558 }
3559 }
3560 if (mContentParent == null) {
3561 mContentParent = generateLayout(mDecor);
3562
3563 // Set up decor part of UI to ignore fitsSystemWindows if appropriate.
3564 mDecor.makeOptionalFitsSystemWindows();
3565
3566 final DecorContentParent decorContentParent = (DecorContentParent) mDecor.findViewById(
3567 R.id.decor_content_parent);
3568
3569 if (decorContentParent != null) {
3570 mDecorContentParent = decorContentParent;
3571 mDecorContentParent.setWindowCallback(getCallback());
3572 if (mDecorContentParent.getTitle() == null) {
3573 mDecorContentParent.setWindowTitle(mTitle);
3574 }
3575
3576 final int localFeatures = getLocalFeatures();
3577 for (int i = 0; i < FEATURE_MAX; i++) {
3578 if ((localFeatures & (1 << i)) != 0) {
3579 mDecorContentParent.initFeature(i);
3580 }
3581 } 3582
3583 mDecorContentParent.setUiOptions(mUiOptions);
3584
            //setIcon,setLogo ...
3608 } else {
            //setTitle ...3627
}
           //...
}
3686 }

installDecor 主要干两件事情

  1. 如果mDecor没有初始化generateDecor()初始化
  2. 如果mContentParent 没有初始化generateLayout()初始化

generateDecor 就是返回一个新的DecorView

3199    protected DecorView generateDecor() {
3200 return new DecorView(getContext(), -1);
3201 }

这里实例化了DecorView,而DecorView则是PhoneWindow类的一个内部类,继承于FrameLayout,由此可知它也是一个ViewGroup。 
那么DecroView到底充当了什么样的角色呢? 
其实,DecorView是整个ViewTree的最顶层View,它是一个FrameLayout布局,代表了整个应用的界面。在该布局下面,有标题view和内容view这两个子元素,而内容view则是上面提到的mContentParent DecorView
 private final class DecorView extends FrameLayout implements RootViewSurfaceTaker{
//...
}
generateLayout()给activity的根布设置各种属性
protected ViewGroup generateLayout(DecorView decor) {
// Apply data from current theme.
// 从主题文件中获取样式信息
TypedArray a = getWindowStyle(); //... if (a.getBoolean(R.styleable.Window_windowNoTitle, false)) {
requestFeature(FEATURE_NO_TITLE);
} else if (a.getBoolean(R.styleable.Window_windowActionBar, false)) {
// Don't allow an action bar if there is no title.
requestFeature(FEATURE_ACTION_BAR);
} if(...){
...
} // Inflate the window decor.
// 加载窗口布局
int layoutResource;
int features = getLocalFeatures();
// System.out.println("Features: 0x" + Integer.toHexString(features));
if ((features & (1 << FEATURE_SWIPE_TO_DISMISS)) != 0) {
layoutResource = R.layout.screen_swipe_dismiss;
} else if(...){
...
} View in = mLayoutInflater.inflate(layoutResource, null); //加载layoutResource
decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)); //往DecorView中添加子View,即mContentParent
mContentRoot = (ViewGroup) in; ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT); // 这里获取的就是mContentParent
if (contentParent == null) {
throw new RuntimeException("Window couldn't find content container view");
} if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) {
ProgressBar progress = getCircularProgressBar(false);
if (progress != null) {
progress.setIndeterminate(true);
}
} if ((features & (1 << FEATURE_SWIPE_TO_DISMISS)) != 0) {
registerSwipeCallbacks();
} // Remaining setup -- of background and title -- that only applies
// to top-level windows.
//... return contentParent;
}

根据设置的主题样式来设置DecorView的风格,比如说有没有titlebar之类的,接着为DecorView添加子View,而这里的子View则是上面提到的mContentParent,如果上面设置了FEATURE_NO_ACTIONBAR,那么DecorView就只有mContentParent一个子View,所以mContentParent是DecorView本身或者是DecorView的一个子元素。

用一幅图来表示DecorView的结构如下:

小结:DecorView是顶级View,内部有titlebar和contentParent两个子元素,contentParent的id是content,而我们设置的main.xml布局则是contentParent里面的一个子元素。

setConetViewz就是把view 添加到顶层的DecorView里面的contentParent里面

将DecorView添加至Window

每一个Activity组件都有一个关联的Window对象,用来描述一个应用程序窗口。每一个应用程序窗口内部又包含有一个View对象,用来描述应用程序窗口的视图。上文分析了创建DecorView的过程,现在则要把DecorView添加到Window对象中。而要了解这个过程,我们首先要简单先了解一下Activity的创建过程: 
首先,在ActivityThread#handleLaunchActivity中启动Activity,在这里面会调用到Activity#onCreate方法,从而完成上面所述的DecorView创建动作,当onCreate()方法执行完毕,在handleLaunchActivity方法会继续调用到ActivityThread#handleResumeActivity方法,我们看看这个方法的源码:

ActivityThread.java

final void handleResumeActivity(IBinder token, boolean clearHide, boolean isForward) {
//...
ActivityClientRecord r = performResumeActivity(token, clearHide); // 这里会调用到onResume()方法 if (r != null) {
final Activity a = r.activity; //...
if (r.window == null && !a.mFinished && willBeVisible) {
r.window = r.activity.getWindow(); // 获得window对象
View decor = r.window.getDecorView(); // 获得DecorView对象
decor.setVisibility(View.INVISIBLE);
ViewManager wm = a.getWindowManager(); // 获得windowManager对象
WindowManager.LayoutParams l = r.window.getAttributes();
a.mDecor = decor;
l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
l.softInputMode |= forwardBit;
if (a.mVisibleFromClient) {
a.mWindowAdded = true;
wm.addView(decor, l); // 调用addView方法
}
//...
}
}
}
 

android Window(一)从setConetView说起的更多相关文章

  1. Android Window 9问9答

    1.简述一下window是什么?在android体系里 扮演什么角色? 答:window就是一个抽象类,他的实现类是phoneWindow.我们一般通过windowManager 来访问window. ...

  2. android window(二)从getSystemService到WindowManagerGlobal

    在Activity调用getSystemService(WINDOW_SERVICE) 调用的是父类ContextThemeWrapper package android.view; public c ...

  3. android window类

    Android的Window类(一) Android的GUI层并不复杂.它的复杂度类似于WGUI这类基于布局和对话框的GUI,与MFC.QT等大型框架没有可比性,甚至飞漫魏永明的MiniGUI都比它复 ...

  4. android window(四)WindowToken

    在WindowManagerService中有两种常见的Token,WindowToken,和AppWindowToken. WindowToken http://androidxref.com/6. ...

  5. android window(三)lWindow添加流程

    http://androidxref.com/6.0.1_r10/xref/frameworks/base/services/core/java/com/android/server/wm/Windo ...

  6. JS判断请求来自Android手机还是iPhone手机,根据不同的手机跳转到不同的链接。

    <script type="text/javascript">var browser = {versions: function () {var u = navigat ...

  7. 判断iPhone/android手机

    JS判断请求来自Android手机还是iPhone手机,根据不同的手机跳转到不同的链接. var browser = {versions: function () {var u = navigator ...

  8. 一个二维码-->网址-->iOS/Android跳转

    view-source:https://dpx.shopo.com.cn/down.html lmxmn117:~ will.wei$ curl https://dpx.shopo.com.cn/do ...

  9. Android KITKAT 以上实现沉浸式状态栏

    extends:http://www.jianshu.com/p/f8374d6267ef 代码未行,效果先上 Flyme4.2 Android4.4.4上运行效果 如何实现 在 KITKAT 之后, ...

随机推荐

  1. 函数的返回值是如何带出和接收的以及内存中的活动情况.RP

    函数返回值时,要生成一个值的副本. 而用引用返回值时,不生成值的副本. 例如,下面的程序是有关引用返回的4种形式: //********************* //** ch9_6.cpp ** ...

  2. C#关于如何修改项目文件夹名称

    在C# 中修改了解决方案项目名称之后,重命名之后出现错误形式: 解决方法就是 打开你的sln文件 ,将里面的地址改过来就好了 以记事本的方式打开.sln文件 修改其中的相对路径,下图选中部分的路径,这 ...

  3. 【Arcgis android】 离线编辑实现及一些代码段

    Arcgis android 离线编辑实现及一些代码段 底图添加 private String path="file:///mnt/sdcard/data/chinasimple.tpk&q ...

  4. winform GDI基础(三)实现画笔

    在程序窗口上使用鼠标画图 private Point pStart, pEnd; private bool isAllowDraw = false; private bool isOpenPen = ...

  5. javascript 设计模式实践之策略模式--输入验证

    博客地址:http://www.cnblogs.com/kongxianghai/p/4985122.html,写的挺好的推荐下!

  6. C/C++ 遇到0xcccccccc访问冲突

    最近一直在纠结这个问题. 最近写代码,总是遇到这个问题,一旦遇到这个问题,以前好使的代码也就不好使了.很费解,上网搜集了下资料.... 这个0xcccccccc是DEBUG模式下,编译器为没有初始化的 ...

  7. iOS10 新特性-新功能,以及ReplayKit库

    iOS的10.0 本文总结了iOS的10,运行于目前推出iOS设备推出的主要开发者相关的功能. iOS版10引入了新的方法来增加您的应用程序通过帮助系统参与在适当的时候建议你的应用程序给用户.如果你在 ...

  8. 《图解HTTP》阅读笔记--第四章--HTTP状态码

    第四章.返回结果的HTTP状态码前言:状态码的职责是告诉用户服务器端描述返回的请求,以便用户判断服务器处理是否正常. 状态码由三位数字和原因短语组成,其中三位数字的首位指定了响应类别:---1xx 接 ...

  9. 20.包含min函数的栈

    题目描述:   定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 思路分析:   设置两个栈,一个push,一个放置最小元素. 代码: impor ...

  10. for循环删除list中多个元素出现的误区

    错误的做法是: for(int i= 0;i<list.size();i++) 因为在你删除元素的同时list.size()也在变小,这样就会照成越界. 正确做法:因为要删除list里面的多个的 ...