標籤:sch ted 介面 tac features roi .net creat protected
轉載請標明出處:http://blog.csdn.net/lmj623565791/article/details/41894125,本文出自:【張鴻洋的部落格】
大家在平時的開發中。對於setContentView肯定不陌生,那麼對其內部的實現會不會比較好奇呢~~~有幸最終能看到一些PhoneWindow神馬的原始碼,今天就帶大家來跑一回原始碼~~
1、Activity setContentView
首先不用說,進入Activity的setContentView
public void setContentView(int layoutResID) { getWindow().setContentView(layoutResID); initActionBar(); }
能夠看到裡面擷取了Window。然後調用了Window的setContentView
2、PhoneWindow setContentView
這裡的Window的實作類別是PhoneWindow(package com.android.internal.policy.impl;)。我們直接看它的實現:
@Override public void setContentView(int layoutResID) { if (mContentParent == null) { installDecor(); } else { mContentParent.removeAllViews(); } mLayoutInflater.inflate(layoutResID, mContentParent); final Callback cb = getCallback(); if (cb != null && !isDestroyed()) { cb.onContentChanged(); } }
能夠看到,首先推斷mContentParent是否為null,是則調用installDecor(),否則移除其內部全部的子Views,然後通過LayoutInflater.inflate將我們傳入的layout放置到mContentParent中。
從這裡就能看出來mContentParent是個ViewGroup且包裹我們整個布局檔案;而installDecor()預計就是去初始化我們這個mContentParent。一會我們會去驗證。
接下來,通過getCallBack拿到了一個CallBack對象。事實上這個擷取到的這個CallBack就是我們Activity自己,你能夠去看我們的Activity是實現了CallBack介面的。
這個Callback明顯就是一個回調,當PhoneWindow接收到系統分發給它的觸摸、IO、菜單等相關的事件時,能夠回調對應的Activity進行處理。至於Callback能夠回調哪些方法,自己看下這個介面的聲明方法就可以。
當然了這裡不是我們的關鍵,由於我們的setContentView裡面僅僅是回調了onContentChanged,而onContentChanged在Activity中是空實現。
好了,接下來去看我們的installDecor()
3、PhoneWindow installDecor
private void installDecor() { if (mDecor == null) { mDecor = generateDecor(); mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS); mDecor.setIsRootNamespace(true); //... } } if (mContentParent == null) { mContentParent = generateLayout(mDecor); mTitleView = (TextView)findViewById(com.android.internal.R.id.title); if (mTitleView != null) { //依據FEATURE_NO_TITLE隱藏,或者設定mTitleView的值 //... } else { mActionBar = (ActionBarView) findViewById(com.android.internal.R.id.action_bar); if (mActionBar != null) { //設定ActionBar標題、表徵圖神馬的;依據FEATURE初始化Actionbar的一些顯示 //... } } }}
這裡代碼比較長,刪除了一些初始化Actionbar樣式神馬的代碼。
能夠看到這裡不僅初始化mContentParent,並且在之前先調用generateDecor();初始化了一個mDecor,mDecor是DecorView對象。為FrameLayout的子類。
在得到mDecor以後設定其焦點的擷取方式為,當其子孫都不須要時,自己才擷取。
然後通過 generateLayout(mDecor);把mDecor做為參數傳入。然後擷取到了我們的mContentParent;
接下裡就開始通過findViewById進行擷取控制項了,而這裡的findViewById的代碼是這種:
public View findViewById(int id) { return getDecorView().findViewById(id); }
getDecorView返回的就是我們的mDecor。
這裡我們推測下,首先去初始化mDecor,然後通過mDecor初始化了mContentParent。接下來mDecor就能夠使用findViewById方法了。那麼我認為,在初始化mDecor的方法
generateDecor()中。一定為我們的mDecor放入了布局或者控制項(最簡單的就是使用inflate壓入了布局檔案)。而mContentParent可能就是mDecor中的某個子View。
是不是這樣呢?
我們一起來先看看generateDecor()方法的實現:
4、PhoneWindow generateDecor
protected DecorView generateDecor() { return new DecorView(getContext(), -1); }
public DecorView(Context context, int featureId) { super(context); mFeatureId = featureId; }
非常遺憾,我們的generateDecor()僅僅是初始化了一個FrameLayout對象,並沒有在其內部壓入布局檔案,看來我們的推測有些問題;只是沒事,既然此方法沒有,那麼generateLayout(mDecor);中一定設定了layout檔案。並且這名字也非常像這麼回事。
5、PhoneWindow generateLayout
protected ViewGroup generateLayout(DecorView decor) { // Apply data from current theme. TypedArray a = getWindowStyle(); //...Window_windowIsFloating,Window_windowNoTitle。Window_windowActionBar... //首先通過WindowStyle中設定的各種屬性。對Window進行requestFeature或者setFlags if (a.getBoolean(com.android.internal.R.styleable.Window_windowNoTitle, false)) { requestFeature(FEATURE_NO_TITLE); } //... if (a.getBoolean(com.android.internal.R.styleable.Window_windowFullscreen, false)) { setFlags(FLAG_FULLSCREEN, FLAG_FULLSCREEN & (~getForcedWindowFlags())); } //...依據當前sdk的版本號碼確定是否須要menukey WindowManager.LayoutParams params = getAttributes(); //通過a中設定的屬性,設定 params.softInputMode 軟鍵盤的模式; //假設當前是浮動Activity。在params中設定FLAG_DIM_BEHIND並記錄dimAmount的值。 //以及在params.windowAnimations記錄WindowAnimationStyle // Inflate the window decor. int layoutResource; int features = getLocalFeatures(); // System.out.println("Features: 0x" + Integer.toHexString(features)); if ((features & ((1 << FEATURE_LEFT_ICON) | (1 << FEATURE_RIGHT_ICON))) != 0) { if (mIsFloating) { TypedValue res = new TypedValue(); getContext().getTheme().resolveAttribute( com.android.internal.R.attr.dialogTitleIconsDecorLayout, res, true); layoutResource = res.resourceId; } else { layoutResource = com.android.internal.R.layout.screen_title_icons; } // XXX Remove this once action bar supports these features. removeFeature(FEATURE_ACTION_BAR); // System.out.println("Title Icons!"); } else if ((features & ((1 << FEATURE_PROGRESS) | (1 << FEATURE_INDETERMINATE_PROGRESS))) != 0 && (features & (1 << FEATURE_ACTION_BAR)) == 0) { // Special case for a window with only a progress bar (and title). // XXX Need to have a no-title version of embedded windows. layoutResource = com.android.internal.R.layout.screen_progress; // System.out.println("Progress!"); } else if ((features & (1 << FEATURE_CUSTOM_TITLE)) != 0) { // Special case for a window with a custom title. // If the window is floating, we need a dialog layout if (mIsFloating) { TypedValue res = new TypedValue(); getContext().getTheme().resolveAttribute( com.android.internal.R.attr.dialogCustomTitleDecorLayout, res, true); layoutResource = res.resourceId; } else { layoutResource = com.android.internal.R.layout.screen_custom_title; } // XXX Remove this once action bar supports these features. removeFeature(FEATURE_ACTION_BAR); } else if ((features & (1 << FEATURE_NO_TITLE)) == 0) { // If no other features and not embedded, only need a title. // If the window is floating, we need a dialog layout if (mIsFloating) { TypedValue res = new TypedValue(); getContext().getTheme().resolveAttribute( com.android.internal.R.attr.dialogTitleDecorLayout, res, true); layoutResource = res.resourceId; } else if ((features & (1 << FEATURE_ACTION_BAR)) != 0) { layoutResource = com.android.internal.R.layout.screen_action_bar; } else { layoutResource = com.android.internal.R.layout.screen_title; } // System.out.println("Title!"); } else if ((features & (1 << FEATURE_ACTION_MODE_OVERLAY)) != 0) { layoutResource = com.android.internal.R.layout.screen_simple_overlay_action_mode; } else { // Embedded, so no decoration is needed. layoutResource = com.android.internal.R.layout.screen_simple; // System.out.println("Simple!"); } View in = mLayoutInflater.inflate(layoutResource, null); decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT)); ViewGroup contentParent = (ViewGroup)findViewById(ID_ANDROID_CONTENT); //... return contentParent; } }
代碼也比較長。首先getWindowStyle在當前的Window的theme中擷取我們的Window中定義的屬性。詳細參考:\frameworks\base\core\res\res\values\attrs.xml
<!-- The set of attributes that describe a Windows‘s theme. --> <declare-styleable name="Window"> <attr name="windowBackground" /> <attr name="windowContentOverlay" /> <attr name="windowFrame" /> <attr name="windowNoTitle" /> <attr name="windowFullscreen" /> <attr name="windowOverscan" /> <attr name="windowIsFloating" /> <attr name="windowIsTranslucent" /> <attr name="windowShowWallpaper" /> <attr name="windowAnimationStyle" /> <attr name="windowSoftInputMode" /> <attr name="windowDisablePreview" /> <attr name="windowNoDisplay" /> <attr name="textColor" /> <attr name="backgroundDimEnabled" /> <attr name="backgroundDimAmount" />
然後就依據這些屬性的值。對我們的Window各種requestFeature,setFlags等等。
所以這裡就是解析我們為Activity設定theme的地方,至於theme一般能夠在AndroidManifest裡面進行設定。
接下來就到關鍵的部分了。21-75行:通過對features和mIsFloating的推斷,為layoutResource進行賦值,至於值能夠為R.layout.screen_custom_title;R.layout.screen_action_bar;等等。
至於features。除了theme中設定的,我們也能夠在Activity的onCreate的setContentView之前進行requestFeature,也解釋了,為什麼須要在setContentView前調用requestFeature設定全屏什麼的。
得到了layoutResource以後,78行,通過LayoutInflater把布局轉化成view,增加到我們的decor,即傳入的mDecor中。
接下來81行:通過mDecor.findViewById傳入R.id.content(相信這個id大家或多或少都聽說過),返回mDecor(布局)中的id為content的View,一般為FrameLayout。
好了。能夠看到我們的mDecor是一個FrameLayout,然後會依據theme去選擇系統中的布局檔案。將布局檔案通過inflate轉化為view。增加到mDecor中。這些布局檔案裡都包括一個id為content的FrameLayout。將其引用返回給mContentParent。
等我們的mContentParent有值了以後。還記得幹嘛了嗎?再貼一次PhoneWindow的setContentView
@Override public void setContentView(int layoutResID) { if (mContentParent == null) { installDecor(); } else { mContentParent.removeAllViews(); } mLayoutInflater.inflate(layoutResID, mContentParent); final Callback cb = getCallback(); if (cb != null && !isDestroyed()) { cb.onContentChanged(); } }
有了mContentParent,然後把我們寫的布局檔案通過inflater增加到mContentParent中。
關於R.layout.xxx能夠在frameworks\base\core\res\res\layout裡面進行查看。
比如:R.layout.screen_custom_title.xml
<?xml version="1.0" encoding="utf-8"?><!--This is a custom layout for a screen.--><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:fitsSystemWindows="true"> <!-- Popout bar for action modes --> <ViewStub android:id="@+id/action_mode_bar_stub" android:inflatedId="@+id/action_mode_bar" android:layout="@layout/action_mode_bar" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/title_container" android:layout_width="match_parent" android:layout_height="?
android:attr/windowTitleSize" style="?android:attr/windowTitleBackgroundStyle"> </FrameLayout> <FrameLayout android:id="@android:id/content" android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="1" android:foregroundGravity="fill_horizontal|top" android:foreground="?android:attr/windowContentOverlay" /></LinearLayout>
上面的title_container是用來放自己定義Title的容器,而以下的content就是放置我們設定的布局的容器。關於自己定義Title範例,大家能夠百度下。
到此,我們的setContentView就分析完畢了。我們能夠回想一下:
首先初始化mDecor,即DecorView為FrameLayout的子類。就是我們整個表單的根視圖了。
然後。依據theme中的屬性值,選擇合適的布局。通過infalter.inflater放入到我們的mDecor中。
在這些布局中,通常會包括ActionBar。Title,和一個id為content的FrameLayout。
最後,我們在Activity中設定的布局,會通過infalter.inflater壓入到我們的id為content的FrameLayout中去。
----------------------------------------------------------------------------------------------------------
博主部分視頻已經上線。假設你不喜歡枯燥的文本。請猛戳(初錄,期待您的支援):
1、Android 自己定義控制項實戰 電商活動中的刮刮卡
2、Android自己定義控制項實戰 打造Android流式布局和熱門標籤
3、Android智能機器人“小慕”的實現
4、高仿QQ5.0側滑
5、高仿5.2.1主介面及訊息提醒
Android 原始碼解析 之 setContentView