1. 什么是Activity
-
Activity 并不负责视图控制,它只是控制生命周期和处理事件。真正控制视图的是Window。一个Activity 包含了一个Window,Window才是真正代表一个窗口。 -
Activity就像一个控制器,统筹视图的添加与显示,以及通过其他回调方法,来与Window、以及View 进行交互。
2. 什么是Window
-
Window 是一个抽象类,实际在Activity 中持有的是其子类PhoneWindow。PhoneWindow 中有个内部类DecorView,通过创建DecorView 来加载Activity 中设置的布局R.layout.activity_main -
Window 是视图的承载器,内部持有一个DecorView,而这个DecorView才是view 的根布局。 -
Window 通过WindowManager 将DecorView 加载其中,并将DecorView 交给ViewRoot,进行视图绘制以及其他交互
3. 什么是DecorView
DecorView 是 FrameLayout 的子类,它可以被认为是 Android 视图树的根节点视图。
-
DecorView 作为顶级 View,一般情况下它内部包含一个竖直方向的 LinearLayout,在这个 LinearLayout 里面有上下三个部分,上面是个 ViewStub,延迟加载的视图(应该是设置ActionBar,根据 Theme 设置),中间的是标题栏(根据Theme设置,有的布局没有),下面的是内容栏。 - 具体情况和Android版本及主体有关,以其中一个布局为例,如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:fitsSystemWindows="true"
android:orientation="vertical">
<!-- Popout bar for action modes -->
<ViewStub
android:id="@+id/action_mode_bar_stub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inflatedId="@+id/action_mode_bar"
android:layout="@layout/action_mode_bar"
android:theme="?attr/actionBarTheme" />
<FrameLayoutstyle="?android:attr/windowTitleBackgroundStyle"
android:layout_width="match_parent"
android:layout_height="?android:attr/windowTitleSize">
<TextView
android:id="@android:id/title"style="?android:attr/windowTitleStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:fadingEdge="horizontal"
android:gravity="center_vertical" />
</FrameLayout>
<FrameLayout
android:id="@android:id/content"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:foreground="?android:attr/windowContentOverlay"
android:foregroundGravity="fill_horizontal|top" />
</LinearLayout>
在 Activity 中通过 setContentView 所设置的布局文件其实就是被加到内容栏之中的,成为其唯一子 View,就是上面的 id 为 content 的 FrameLayout 中,在代码中可以通过 content 来得到对应加载的布局。
ViewGroup content = (ViewGroup)findViewById(android.R.id.content);
ViewGroup rootView = (ViewGroup) content.getChildAt(0);
4. 关系结构图
- Activity 与 PhoneWindow 与 DecorView 关系图

5. Window创建过程
- App点击桌面图片启动过程

6. Activity 与 PhoneWindow 与 DecorView 之间什么关系?
一个 Activity 对应一个 Window 也就是 PhoneWindow,一个 PhoneWindow 持有一个 DecorView 的实例,DecorView 本身是一个 FrameLayout。










