android应用开发之Window,View和WindowManager
16lz
2021-01-26
Android Window,View和WindowManager注记
Email: zcatt@163.com
Blog http://zcatt.blog.chinaunix.net
内容提要
Window, View和WindowManager的注记一点.以供备忘和参考。
声明
仅限学习交流,禁止商业用途。转载需注明出处。
版本记录
Date Ver Note
2011-05-24 0.1 Draft. zcatt, Beijing
2011-05-25 0.2 add KeyguardViewHost.
Android中以Window为考察点的话, 涉及的主要接口和类有View, ViewGroup, ViewRoot, Window, PhoneWindow, WindowManagerPolice, PhoneWindowManager, WindowManager, 和WindowManagerImpl.
Window中的View
----------------------
Window是抽象类, PhoneWindow继承实现Window. Android中使用的Window Object实际是PhoneWindow的实例.
Window
PhoneWindow
Window中记录了自己的View, 就是mDecor. DecorView是FrameLayout的子类. mDecor实际是Window中包含的所有View的顶层View.
Window中还记录了一个View, 就是mContentParent, 这就是Window的content view. 实际编程中打交道的都是这个mContentParent. Window在生成自己的mDecor时, 会根据window的属性, 例如是否有title, 是不是dialog等等从预先定义好的layout资源中选择一个载入. 在这些资源中都定义了一个android:id="@android:id/content"的<FrameLayout>. 而这个FrameLayout就是mContentParent指向的layout对象. 可参看installDecor()@PhoneWindow.java. 而setContentView(yourView,...)实际就是将yourView挂接到mContentParent.
ViewRoot, ViewGroup,和View
------------------------------
View的成员变量中有一个指向parent的变量. 而ViewGroup是View的子类,除了有parent, 还有指向自己容纳的子view的mChildren. 而ViewRoot的mView则指向自己唯一的一个子view.
这3个类联合使用就可以组建成一颗颗view的树. ViewRoot是这个树的根点, 各种ViewGroup是树枝, 而各种view是树叶. 在这个树中, ViewRoot的作用很特殊需要特别说明. 对于树的操作通常是从ViewRoot的根节点发动的, 例如requestLayout(), 实际是由ViewRoot的performTraversals()完成.
WindowManager, ViewRoot和View
-------------------------------
WindowManagerImpl是WindowManager的的实现. 它主要用于记录一个3元对应关系<View, ViewRoot, WindowManager.LayoutParams>. 使用addView()加入WindowManger的yourView, 都会自动生成一个ViewRoot做为根点. 这通常是在activity的create后的第一次resume中完成, 而这个view就是window的decor view, 见前. 参考handleResumeActivity()@ActivityThread.java.
public final class ViewRoot extends Handler implements ViewParent
, View.AttachInfo.Callbacks
ViewRoot的所有作用, 或者说它的作用, 就在于它是view树的root. 而且ViewRoot是Handler, 在这里完成相关message的处理工作. 包括的message code如下, 引自源代码.
Acitivity, Window和View
------------------------------
Acitivity在创建后的attach()@Activity.java中创建自己的Window, 继而生成自己的WindowManager. 实际上这个WindowManager都是对process全局的一个singlton WindowManger的wrapper封装. 我们暂称这个singleton WindowManager为核心WindowManager. Activity在首次resume时会将自己的decor view加入到核心WindowManager的记录中. (一个例外, 除了decor view, KeyguardViewHost也会调用WindowManager.addview()加入到核心WindowManager记录中)
这样, 核心WindowManager中记录了process中所有已经首次resume后activity的decore view. '首次resume'实际也是表征了activity应当拥有view了, 可以用于显示了这个概念.
Email: zcatt@163.com
Blog http://zcatt.blog.chinaunix.net
内容提要
Window, View和WindowManager的注记一点.以供备忘和参考。
声明
仅限学习交流,禁止商业用途。转载需注明出处。
版本记录
Date Ver Note
2011-05-24 0.1 Draft. zcatt, Beijing
2011-05-25 0.2 add KeyguardViewHost.
Android中以Window为考察点的话, 涉及的主要接口和类有View, ViewGroup, ViewRoot, Window, PhoneWindow, WindowManagerPolice, PhoneWindowManager, WindowManager, 和WindowManagerImpl.
Window中的View
----------------------
Window是抽象类, PhoneWindow继承实现Window. Android中使用的Window Object实际是PhoneWindow的实例.
Window
PhoneWindow
Window中记录了自己的View, 就是mDecor. DecorView是FrameLayout的子类. mDecor实际是Window中包含的所有View的顶层View.
- // This is the top-level view of the window, containing the window decor.
- privateDecorView mDecor;
- // This is the view in which the window contents are placed. It is either
- // mDecor itself, or a child of mDecor where the contents go.
- privateViewGroup mContentParent;
- //... ...
- privatefinalclassDecorViewextendsFrameLayout{
- //....
- }
Window中还记录了一个View, 就是mContentParent, 这就是Window的content view. 实际编程中打交道的都是这个mContentParent. Window在生成自己的mDecor时, 会根据window的属性, 例如是否有title, 是不是dialog等等从预先定义好的layout资源中选择一个载入. 在这些资源中都定义了一个android:id="@android:id/content"的<FrameLayout>. 而这个FrameLayout就是mContentParent指向的layout对象. 可参看installDecor()@PhoneWindow.java. 而setContentView(yourView,...)实际就是将yourView挂接到mContentParent.
ViewRoot, ViewGroup,和View
------------------------------
View的成员变量中有一个指向parent的变量. 而ViewGroup是View的子类,除了有parent, 还有指向自己容纳的子view的mChildren. 而ViewRoot的mView则指向自己唯一的一个子view.
这3个类联合使用就可以组建成一颗颗view的树. ViewRoot是这个树的根点, 各种ViewGroup是树枝, 而各种view是树叶. 在这个树中, ViewRoot的作用很特殊需要特别说明. 对于树的操作通常是从ViewRoot的根节点发动的, 例如requestLayout(), 实际是由ViewRoot的performTraversals()完成.
WindowManager, ViewRoot和View
-------------------------------
WindowManagerImpl是WindowManager的的实现. 它主要用于记录一个3元对应关系<View, ViewRoot, WindowManager.LayoutParams>. 使用addView()加入WindowManger的yourView, 都会自动生成一个ViewRoot做为根点. 这通常是在activity的create后的第一次resume中完成, 而这个view就是window的decor view, 见前. 参考handleResumeActivity()@ActivityThread.java.
public final class ViewRoot extends Handler implements ViewParent
, View.AttachInfo.Callbacks
ViewRoot的所有作用, 或者说它的作用, 就在于它是view树的root. 而且ViewRoot是Handler, 在这里完成相关message的处理工作. 包括的message code如下, 引自源代码.
- publicfinalstaticintDO_TRAVERSAL=1000;
- publicfinalstaticintDIE=1001;
- publicfinalstaticintRESIZED=1002;
- publicfinalstaticintRESIZED_REPORT=1003;
- publicfinalstaticintWINDOW_FOCUS_CHANGED=1004;
- publicfinalstaticintDISPATCH_KEY=1005;
- publicfinalstaticintDISPATCH_POINTER=1006;
- publicfinalstaticintDISPATCH_TRACKBALL=1007;
- publicfinalstaticintDISPATCH_APP_VISIBILITY=1008;
- publicfinalstaticintDISPATCH_GET_NEW_SURFACE=1009;
- publicfinalstaticintFINISHED_EVENT=1010;
- publicfinalstaticintDISPATCH_KEY_FROM_IME=1011;
- publicfinalstaticintFINISH_INPUT_CONNECTION=1012;
- publicfinalstaticintCHECK_FOCUS=1013;
- publicfinalstaticintCLOSE_SYSTEM_DIALOGS=1014;
Acitivity, Window和View
------------------------------
Acitivity在创建后的attach()@Activity.java中创建自己的Window, 继而生成自己的WindowManager. 实际上这个WindowManager都是对process全局的一个singlton WindowManger的wrapper封装. 我们暂称这个singleton WindowManager为核心WindowManager. Activity在首次resume时会将自己的decor view加入到核心WindowManager的记录中. (一个例外, 除了decor view, KeyguardViewHost也会调用WindowManager.addview()加入到核心WindowManager记录中)
这样, 核心WindowManager中记录了process中所有已经首次resume后activity的decore view. '首次resume'实际也是表征了activity应当拥有view了, 可以用于显示了这个概念.
更多相关文章
- Android(安卓)学习记录—新家
- Android之插入通话记录,短信箱插入短信,号码薄插入联系人
- Android(安卓)App 退出整个应用
- TabActivity子类中处理返回键事件
- android 开发中遇到未知问题的记录
- Android(安卓)ApiDemos示例解析(124):Views->ImageSwitcher
- 《Android(安卓)JNI》04 对java传进来的数组进行操作并返回
- Android(安卓)Resource介绍和使用以及bug记录
- 安卓:View的基本信息