Android 之Activity , Window跟View的关系及一些细节

Android 之Activity , Window和View的关系及一些细节
跟踪Activity的源码就会发现:
Activity.attch() -> PolicyManager -> Policy -> PhoneWindow -> mLayoutInflater.inflate()&mContentParent.addView()
这只是一个简单的跟踪过程描述。通过跟踪源代码,就可以很清晰的看出他们三者的关系。 

Activity像一个工匠(控制单元),Window像窗户(承载模型),View像窗花(显示视图)
LayoutInflater像剪刀,Xml配置像窗花图纸。

1)一个Activity构造的时候会初始化一个Window,准确的说是PhoneWindow。
2)这个PhoneWindow有一个“ViewRoot”,引号是说其实这个“ViewRoot”是一个View或者说ViewGroup,是最初始的根视图。
3)“ViewRoot”通过addView方法来一个个的添加View。比如TextView,Button等

4)这些View的事件监听,是由WindowManagerService来接受消息,并且回调Activity函数。比如onClickListener,onKeyDown等


Activity是android的显示视图么?
不是


LayoutInflater是做什么的 ? layoutInflater.inflater()做什么的?
一般来讲,我们用LayoutInflater做一件事:inflate。inflate这个方法总共有四种形式,目的都是把xml表述的layout转化为View。
This class is used to instantiate layout XML file into its corresponding View objects . It is never be used directly -- use 
getLayoutInflater() or getSystemService(String)getLayoutInflater() or getSystemService(String) to retrieve a standard 
LayoutInflater instance that is already hooked up to the current context and correctly configured for the device you are 
running on