編輯:關於 Android 開發
本文從 Activity 的 setContentView 方法講起,逐層剖析 Android GUI 系統中 View 體系的構建過程。
public void setContentView(@LayoutRes int layoutResID) {
getWindow().setContentView(layoutResID);
initWindowDecorActionBar();
}
首先要問:這個 getWindow 是什麼?其實它就是 mWindow,在 attach 方法裡定義:
mWindow = new PhoneWindow(this);
預設就是 PhoneWindow,也就是 Activity 裡包含的 window 實例。
接著看 PhoneWindow 的 setContentView:
@Override public void setContentView(int layoutResID) {
if (mContentParent == null) {
installDecor();
} else if (!hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
mContentParent.removeAllViews();
}
if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
final Scene newScene = Scene.getSceneForLayout(mContentParent, layoutResID, getContext());
transitionTo(newScene);
} else {
mLayoutInflater.inflate(layoutResID, mContentParent);
}
mContentParent.requestApplyInsets();
final Callback cb = getCallback();
if (cb != null && !isDestroyed()) {
cb.onContentChanged();
}
}
那 mContentParent 是什麼?看它的定義:
// 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.
按註釋的意思,mContentParent 就是我們加入 layout 的父布局,它有兩種可能:DecorView 本身,或它的子 View。
installDecor 主要做兩件事:第一,若需要則建立 Decor;第二,建立 mContentParent:
mContentParent = generateLayout(mDecor);
來看 generateLayout。它有大量判斷,但最終把 layout 加入 Decor 的過程其實就兩句:
View in = mLayoutInflater.inflate(layoutResource, null); decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
第一句把 layout id 變換成 View,第二句把 View 加入到 Decor 中。
Activity 中的 view,是透過 Window 然後跟 WMS(WindowManagerService)來管理的。一個 Activity 對象持有 mWindow 對象,其管理路徑如下:
mWindow -> WindowManager -> WindowManagerImpl -> WMS -> ViewRoot -> addView。
【微學堂】第22期:Nginx 的性能優化
【微學堂】第22期:Nginx 的性能優化分享主題:Nginx 的性能優化分享時間:2016 年 11 月 24 日(周四)20:00特邀講師:李強(撒加)講師簡介:運維
Android 補間動畫入門:透明度、縮放、位移、旋轉與組合動畫
Android 補間動畫入門:透明度、縮放、位移、旋轉與組合動畫 補間動畫是 Android 動畫體系中常見的一種。它只需要指定動畫的起點與終點,中間幀由系統補齊。相比逐
TabLayout 與 ViewPager 雙向聯動:點 Tab 換頁、滑頁面換 Tab
TabLayout 與 ViewPager 雙向聯動:點 Tab 換頁、滑頁面換 Tab android.support.design.widget.TabLayout
從相親策略看「線上最大值問題」:37% 法則的由來與證明
從相親策略看「線上最大值問題」:37% 法則的由來與證明 問題設定 假設你想找一位交往對象,前面幾次嘗試都不順,於是決定找相親仲介。仲介每天推薦一位人選,你面談後決定要不