ViewManager  vm = a.getWindowManager();

vm.add(view,l);

window :一个抽象的窗口基类,控制顶层窗口的外观和行为。作为顶层窗口,可控制窗口背景、和标题。默认的案件处理等,他作为一个顶层的View加入到WIndowManager。实际中用的比较多的是实现类比如针对手机的phoneWindow,针对平板的MidWindow 。

View:一个UI的单元,暂居一定的区域可用于绘制,并可以处理事件。

windowManager实际上的基类是LocalWindowManager

 
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.

  1. // This is the top-level view of the window, containing the window decor.
  2. private DecorView mDecor;
  3. // This is the view in which the window contents are placed. It is either
  4. // mDecor itself, or a child of mDecor where the contents go.
  5. private ViewGroup mContentParent;
  6. //... ...
  7. private final class DecorView extends FrameLayout {
  8. //....
  9. }

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如下, 引自源代码.

  1. public final static int DO_TRAVERSAL = 1000;
  2. public final static int DIE = 1001;
  3. public final static int RESIZED = 1002;
  4. public final static int RESIZED_REPORT = 1003;
  5. public final static int WINDOW_FOCUS_CHANGED = 1004;
  6. public final static int DISPATCH_KEY = 1005;
  7. public final static int DISPATCH_POINTER = 1006;
  8. public final static int DISPATCH_TRACKBALL = 1007;
  9. public final static int DISPATCH_APP_VISIBILITY = 1008;
  10. public final static int DISPATCH_GET_NEW_SURFACE = 1009;
  11. public final static int FINISHED_EVENT = 1010;
  12. public final static int DISPATCH_KEY_FROM_IME = 1011;
  13. public final static int FINISH_INPUT_CONNECTION = 1012;
  14. public final static int CHECK_FOCUS = 1013;
  15. public final static int CLOSE_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了, 可以用于显示了这个概念.

http://meiyitianabc.blog.163.com/blog/static/10502212720119931811881/

android应用开发之Window,View和WindowManager .的更多相关文章

  1. Android混合开发之WebViewJavascriptBridge实现JS与java安全交互

    前言: 为了加快开发效率,目前公司一些功能使用H5开发,这里难免会用到Js与Java函数互相调用的问题,这个Android是提供了原生支持的,不过存在安全隐患,今天我们来学习一种安全方式来满足Js与j ...

  2. Android混合开发之WebView与Javascript交互

    前言: 最近公司的App为了加快开发效率选择了一部分功能采用H5开发,从目前市面的大部分App来讲,大致分成Native App.Web App.Hybrid App三种方式,个人觉得目前以Hybri ...

  3. Android安全开发之WebView中的地雷

    Android安全开发之WebView中的地雷 0X01 About WebView 在Android开发中,经常会使用WebView来实现WEB页面的展示,在Activiry中启动自己的浏览器,或者 ...

  4. android软件开发之webView.addJavascriptInterface循环渐进【二】

    本篇文章由:http://www.sollyu.com/android-software-development-webview-addjavascriptinterface-cycle-of-gra ...

  5. android软件开发之webView.addJavascriptInterface循环渐进【一】

    本篇文章由:http://www.sollyu.com/android-software-development-webview-addjavascriptinterface-cycle-of-gra ...

  6. Android混合开发之WebView使用总结

    前言: 今天修改项目中一个有关WebView使用的bug,激起了我总结WebView的动机,今天抽空做个总结. 混合开发相关博客: Android混合开发之WebView使用总结 Android混合开 ...

  7. Android 异步开发之 AsyncQueryHandler 批量添加联系人

    AsyncQueryHandler: 官方解释是一个异步帮助类(A helper class to help make handling asynchronous ContentResolver qu ...

  8. Android安全开发之ZIP文件目录遍历

    1.ZIP文件目录遍历简介 因为ZIP压缩包文件中允许存在“../”的字符串,攻击者可以利用多个“../”在解压时改变ZIP包中某个文件的存放位置,覆盖掉应用原有的文件.如果被覆盖掉的文件是动态链接s ...

  9. Android驱动开发之Hello实例

    Android驱动开发之Hello实例:   驱动部分 modified:   kernel/arch/arm/configs/msm8909-1gb_w100_hd720p-perf_defconf ...

随机推荐

  1. Linux系统编程(25)——终端

    在Linux系统中,用户通过终端登录系统后得到一个Shell进程,这个终端成为Shell进程的控制终端.控制终端是保存在PCB中的信息,而我们知道fork会复制PCB中的信息,因此由Shell进程启动 ...

  2. UML建模之状态图(Statechart Diagram)

     状态图目录: 一.状态图简介(Brief introduction) 二.状态图元素(State Diagram Elements) 1.状态(States) 2.转移(Transitions) 3 ...

  3. window.open的小技巧分享

        今天再次谈起window.open是因为发现了一个比较好玩的小技巧,详细内容我们稍后详细说明.       聊到window.open,不得不说明一下他的使用方法,主要有两种形式:   win ...

  4. spring读取properties文件

    1.方式一 <util:properties id="meta" location="classpath:config/metainfo.properties&qu ...

  5. ubuntu 创建eclipse 快捷方式

    Ubuntu 上链接快捷方式,将想面内容复制并修改,命名为:eclipse.desktop #!/usr/bin/env xdg-open [Desktop Entry] Name=Eclipse C ...

  6. 互联网程序设计c++

    地址:ftp.sist.stdu.edu.cn用户名:lzh_hlw20133密码:lzhstdftp端口:2014

  7. google在线測试练习题1

    Problem You receive a credit C at a local store and would like to buy two items. You first walk thro ...

  8. resin4 简单学习

    1.Resin 4.0.26 Resin是CAUCHO公司的产品,是一个非常流行的application server,对servlet和JSP提供了良好的支持,性能也比较优良,resin自身采用JA ...

  9. 苹果的HomeKit协议

    苹果的HomeKit协议非常底层,其作用仅限于让iOS平台和家居设备能够相互“握手”,但“认识”之后,想要继续控制灯.空调等设备,仍然需要家电厂商在HomeKit的基础上进行二次开发.

  10. Java基础知识强化57:经典排序之希尔排序(ShellSort)

    1. 希尔排序的原理: 希尔排序(Shell Sort)是插入排序的一种.也称缩小增量排序,是直接插入排序算法的一种更高效的改进版本.希尔排序是非稳定排序算法.该方法因DL.Shell于1959年提出 ...