Android 之Activity , Window和View的關係及一些細節

來源:互聯網
上載者:User

跟蹤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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.