編輯:關於 Android 程式設計
Tips:此源碼分析基於 Android 4.2。
先來看看一個 Activity 上的 UI 控件結構。

圖 1-1 Activity 中的 UI 組件結構
好了,現在開始分析。
了解 Android 的 Zygote 分裂你會知道,每個 APP 都是 Zygote 的子進程,而他的入口函數是 ActivityThread 類中的 main 函數。其中有一個 handleLaucherActivity 函數,這裡就是創建 Activity 的地方。
private void handleLaunchActivity(ActivityClientRecord r, Intent customIntent) {
// If we are getting ready to gc after going to the background, well
// we are back active so skip it.
unscheduleGcIdler();
if (r.profileFd != null) {
mProfiler.setProfiler(r.profileFile, r.profileFd);
mProfiler.startProfiling();
mProfiler.autoStopProfiler = r.autoStopProfiler;
}
// Make sure we are running with the most recent config.
handleConfigurationChanged(null, null);
if (localLOGV) Slog.v(TAG, "Handling launch of " + r);
// 重點一
Activity a = performLaunchActivity(r, customIntent);
if (a != null) {
r.createdConfig = new Configuration(mConfiguration);
Bundle oldState = r.state;
// 重點二
handleResumeActivity(r.token, false, r.isForward,
!r.activity.mFinished && !r.startsNotResumed);
}
// 後面代碼省略
}
這裡已經標出了兩個重點的函數。
先來看看第一個 performLauncherActivity。這個函數返回一個 activity,可見 activity 確實在這裡創建了,先上代碼。
private Activity performLaunchActivity(ActivityClientRecord r, Intent customIntent) {
// ...
Activity activity = null;
try {
java.lang.ClassLoader cl = r.packageInfo.getClassLoader();
// 正真創建 activity 的地方
activity = mInstrumentation.newActivity(
cl, component.getClassName(), r.intent);
StrictMode.incrementExpectedActivityCount(activity.getClass());
r.intent.setExtrasClassLoader(cl);
if (r.state != null) {
r.state.setClassLoader(cl);
}
} catch (Exception e) {
if (!mInstrumentation.onException(activity, e)) {
throw new RuntimeException(
"Unable to instantiate activity " + component + ": " + e.toString(), e);
}
}
// ...
if (activity != null) {
Context appContext = createBaseContextForActivity(r, activity);
CharSequence title = r.activityInfo.loadLabel(appContext.getPackageManager());
Configuration config = new Configuration(mCompatConfiguration);
if (DEBUG_CONFIGURATION) {
Slog.v(TAG, "Launching activity " + r.activityInfo.name
+ " with config " + config);
}
// 又是一個重點,暫且先不分析
activity.attach(appContext, this, getInstrumentation(), r.token, r.ident,
app, r.intent, r.activityInfo, title, r.parent, r.embeddedID,
r.lastNonConfigurationInstances, config);
if (customIntent != null) {
activity.mIntent = customIntent;
}
r.lastNonConfigurationInstances = null;
activity.mStartedActivity = false;
int theme = r.activityInfo.getThemeResource();
if (theme != 0) {
activity.setTheme(theme);
}
activity.mCalled = false;
// 這裡回調了 Activity 的 OnCreate
mInstrumentation.callActivityOnCreate(activity, r.state);
if (!activity.mCalled) {
throw new SuperNotCalledException(
"Activity " + r.intent.getComponent().toShortString()
+ " did not call through to super.onCreate()");
}
r.activity = activity;
r.stopped = true;
if (!r.activity.mFinished) {
activity.performStart();
r.stopped = false;
}
if (!r.activity.mFinished) {
if (r.state != null) {
mInstrumentation.callActivityOnRestoreInstanceState(activity, r.state);
}
}
if (!r.activity.mFinished) {
activity.mCalled = false;
mInstrumentation.callActivityOnPostCreate(activity, r.state);
if (!activity.mCalled) {
throw new SuperNotCalledException(
"Activity " + r.intent.getComponent().toShortString()
+ " did not call through to super.onPostCreate()");
}
}
}
// ...
return activity;
}
這裡貼上了關鍵的代碼,由此可見 performLauncherActivity 函數主要做了兩件重要的事情:創建了 Activity 以及回調了 OnCreate。
這裡看出它是利用了 Java 的反射機制,根據類名創建了一個 Activity。
接下來再來看下第二個函數 handleResumeActivity,什麼都不說,先看代碼。
final void handleResumeActivity(IBinder token, boolean clearHide,
boolean isForward, boolean reallyResume) {
// ...
if (r.window == null && !a.mFinished && willBeVisible) {
r.window = r.activity.getWindow();
View decor = r.window.getDecorView();
decor.setVisibility(View.INVISIBLE);
ViewManager wm = a.getWindowManager();
WindowManager.LayoutParams l = r.window.getAttributes();
a.mDecor = decor;
l.type = WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
l.softInputMode |= forwardBit;
if (a.mVisibleFromClient) {
a.mWindowAdded = true;
// 關鍵函數
wm.addView(decor, l);
}
// If the window has already been added, but during resume
// we started another activity, then don't yet make the
// window visible.
}
// ...
}
到了這裡可以看到多了兩個比較重要的對象 View、ViewManager,隨後 decor 對象 add 到了 ViewManager。那麼這兩個對象到底是什麼呢?
getDecorView 進去一看,原來是 Window 類的一個抽象方法,那麼到底是什麼實現了它?這就要看剛剛那個 Activity 的 attach 函數了。
final void attach(Context context, ActivityThread aThread, Instrumentation instr,
IBinder token, int ident, Application application, Intent intent,
ActivityInfo info, CharSequence title, Activity parent, String id,
NonConfigurationInstances lastNonConfigurationInstances,
Configuration config) {
attachBaseContext(context);
mFragments.attachActivity(this, mContainer, null);
// 創建了一個 mWindow,這是一個實現了 Window 抽象方法的對象
mWindow = PolicyManager.makeNewWindow(this);
mWindow.setCallback(this);
mWindow.getLayoutInflater().setPrivateFactory(this);
// ...
// 創建了 WindowManager
mWindow.setWindowManager(
(WindowManager) context.getSystemService(Context.WINDOW_SERVICE),
mToken, mComponent.flattenToString(),
(info.flags & ActivityInfo.FLAG_HARDWARE_ACCELERATED) != 0);
if (mParent != null) {
mWindow.setContainer(mParent.getWindow());
}
mWindowManager = mWindow.getWindowManager();
mCurrentConfig = config;
}
在 handleResumeActivity 中的 r.window = r.activity.getWindow(); 我們可以看出,這裡創建的 mWindow 給了 r.window。
public Window getWindow() {
return mWindow;
}
由此可見,mWindow 的 getDecorView 方法返回的便是我們要認識的那個 View。現在就來看看這個 mWindow 到底是何方神聖。
創建 mWindow 時有出現了個 PolicyManager,現在先看看這個是什麼東西。
public final class PolicyManager {
private static final String POLICY_IMPL_CLASS_NAME = "com.android.internal.policy.impl.Policy";
private static final IPolicy sPolicy;
static {
// Pull in the actual implementation of the policy at run-time
try {
Class policyClass = Class.forName(POLICY_IMPL_CLASS_NAME);
sPolicy = (IPolicy) policyClass.newInstance();
} catch (ClassNotFoundException ex) {
throw new RuntimeException(POLICY_IMPL_CLASS_NAME + " could not be loaded", ex);
} catch (InstantiationException ex) {
throw new RuntimeException(POLICY_IMPL_CLASS_NAME + " could not be instantiated", ex);
} catch (IllegalAccessException ex) {
throw new RuntimeException(POLICY_IMPL_CLASS_NAME + " could not be instantiated", ex);
}
}
// Cannot instantiate this class
private PolicyManager() {}
// The static methods to spawn new policy-specific objects
public static Window makeNewWindow(Context context) {
return sPolicy.makeNewWindow(context);
}
public static LayoutInflater makeNewLayoutInflater(Context context) {
return sPolicy.makeNewLayoutInflater(context);
}
public static WindowManagerPolicy makeNewWindowManager() {
return sPolicy.makeNewWindowManager();
}
public static FallbackEventHandler makeNewFallbackEventHandler(Context context) {
return sPolicy.makeNewFallbackEventHandler(context);
}
}
從這裡可以看出,調用的 PolicyManager.makeNewWindow(this) 正真實現是在 Policy 中。
public Window makeNewWindow(Context context) {
return new PhoneWindow(context);
}
至此我們才發現,所謂的 mWindow 其實是一個 PhoneWindow 對象。
現在我們再看看正真實現 getDecorView 的 PhoneWindow 中獲得的 View 到底是什麼?
@Override
public final View getDecorView() {
if (mDecor == null) {
installDecor();
}
return mDecor;
}
private void installDecor() {
if (mDecor == null) {
mDecor = generateDecor();
mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
mDecor.setIsRootNamespace(true);
if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
}
}
if (mContentParent == null) {
mContentParent = generateLayout(mDecor);
// Set up decor part of UI to ignore fitsSystemWindows if appropriate
mDecor.makeOptionalFitsSystemWindows();
mTitleView = (TextView) findViewById(com.android.internal.R.id.title);
// ...
}
// ...
}
到了這一步我們才發現,原來那個 View 是一個 DecorView。
private final class DecorView extends FrameLayout implements RootViewSurfaceTaker
好了,現在解決了我們第一個問題:View 到底是什麼?現在我們再來分析下 ViewManager 究竟是什麼?
ViewManager wm = a.getWindowManager();
查看 Activity 的 getWindowManager 以及 attach 方法,我們會發現這正是 attach 方法中創建的 WindowManager。
public WindowManager getWindowManager() {
return mWindowManager;
}
但是 WindowManager 只是一個公共的接口,我們還是得進入到 attach 中的 mWindow.setWindowManager() 中看看到底發生了什麼?
public interface WindowManager extends ViewManager
public void setWindowManager(WindowManager wm, IBinder appToken, String appName,
boolean hardwareAccelerated) {
mAppToken = appToken;
mAppName = appName;
mHardwareAccelerated = hardwareAccelerated
|| SystemProperties.getBoolean(PROPERTY_HARDWARE_UI, false);
if (wm == null) {
wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
}
mWindowManager = ((WindowManagerImpl) wm).createLocalWindowManager(this);
}
public WindowManagerImpl createLocalWindowManager(Window parentWindow) {
return new WindowManagerImpl(mDisplay, parentWindow);
}
到此我們終於解決了我們的第二個問題:那個 ViewManager 其實是 WindowManagerImpl。
好了,處理了這麼多復雜的關系,我們先來總結下:
1、我們是分析到了 handleResumeActivity 這個函數,想弄清楚 View 跟 ViewManager 分別是什麼?
2、之後我們返回去查看 Activity 的 attach 函數,發現其中創建的 mWindow 跟 mWindowManager 其實是 PhoneWindow 與 WindowManagerImpl。
3、我們再回到 View 與 ViewManager 的創建過程,發現其實 View 是在 PhoneWindow 中創建的 DecorView,而 ViewManager 正是 mWindowManager(即 WindowManagerImpl),在 attach 中調用 Window 的 setWindowManager 時將創建的 WindowManagerImpl 保存了起來。
經過這般分析再來看這張圖:

圖 1-2 Window 與 WindowManager
好了,接下來我們繼續來分析 handleResumeActivity 中另一個很關鍵的函數 wm.addView(decor, l)。
addView 的正真實現是在 frameworks\base\core\java\android\view\WindowManagerGlobal.java。
public void addView(View view, ViewGroup.LayoutParams params, Display display, Window parentWindow) {
if (view == null) {
throw new IllegalArgumentException("view must not be null");
}
if (display == null) {
throw new IllegalArgumentException("display must not be null");
}
if (!(params instanceof WindowManager.LayoutParams)) {
throw new IllegalArgumentException("Params must be WindowManager.LayoutParams");
}
final WindowManager.LayoutParams wparams = (WindowManager.LayoutParams) params;
if (parentWindow != null) {
parentWindow.adjustLayoutParamsForSubWindow(wparams);
}
ViewRootImpl root;
View panelParentView = null;
synchronized (mLock) {
// Start watching for system property changes.
if (mSystemPropertyUpdater == null) {
mSystemPropertyUpdater = new Runnable() {
@Override
public void run() {
synchronized (mLock) {
for (ViewRootImpl viewRoot : mRoots) {
viewRoot.loadSystemProperties();
}
}
}
};
SystemProperties.addChangeCallback(mSystemPropertyUpdater);
}
int index = findViewLocked(view, false);
if (index >= 0) {
throw new IllegalStateException(
"View " + view + " has already been added to the window manager.");
}
// If this is a panel window, then find the window it is being
// attached to for future reference.
if (wparams.type >= WindowManager.LayoutParams.FIRST_SUB_WINDOW
&& wparams.type <= WindowManager.LayoutParams.LAST_SUB_WINDOW) {
final int count = mViews != null ? mViews.length : 0;
for (int i = 0; i < count; i++) {
if (mRoots[i].getWindowToken() == wparams.token) {
panelParentView = mViews[i];
break;
}
}
}
// 這裡出現了一個新的對象 ViewRootImpl
root = new ViewRootImpl(view.getContext(), display);
view.setLayoutParams(wparams);
mViews[index] = view;
mRoots[index] = root;
mParams[index] = wparams;
try {
// 並調用了它的一個重要方法 setView
root.setView(view, wparams, panelParentView);
} catch (RuntimeException e) {
// BadTokenException or new IllegalArgumentException
mViews[index] = null;
mRoots[index] = null;
mParams[index] = null;
synchronized (mLock) {
final int numViews = mViews != null ? mViews.length : 0;
for (int i = numViews - 1; i >= 0; i--) {
if (mRoots[i] == null) {
removeViewLocked(i, true);
}
}
}
throw e;
}
}
}
這裡有出現了一個新的對象 ViewRootImpl 以及調用了它的一個重要方法 setView,現在我們就來分析下。
public final class ViewRootImpl implements ViewParent,
View.AttachInfo.Callbacks, HardwareRenderer.HardwareDrawCallbacks
查看 ViewRootImpl 的源碼可以知道,它裡面包含了幾個重要的成員對象(frameworks\base\core\java\android\view\ViewRootImpl.java):
有一個 mSurface,他是 Surface 類型,而前面提到的 UI 都是在這上面繪畫出來的,可以想象成一個畫布。
還有一個 W 類型的內部類,這個類將參與 Binder 通信,並且重寫了 handleMessage。
static class W extends IWindow.Stub
我們接著看下 ViewRootImpl 的構造函數:
public ViewRootImpl(Context context, Display display) {
super();
if (MEASURE_LATENCY) {
if (lt == null) {
lt = new LatencyTimer(100, 1000);
}
}
// Initialize the statics when this class is first instantiated.
// This is done here instead of in the static block because Zygote does not
// allow the spawning of threads.
mWindowSession = WindowManagerGlobal.getWindowSession(context.getMainLooper());
mDisplay = display;
CompatibilityInfoHolder cih = display.getCompatibilityInfo();
mCompatibilityInfo = cih != null ? cih : new CompatibilityInfoHolder();
mThread = Thread.currentThread();
mLocation = new WindowLeaked(null);
mLocation.fillInStackTrace();
mWidth = -1;
mHeight = -1;
mDirty = new Rect();
mTempRect = new Rect();
mVisRect = new Rect();
mWinFrame = new Rect();
mWindow = new W(this);
// ...
}
這裡一個比較重要的函數是 getWindowSession。
public static IWindowSession getWindowSession(Looper mainLooper) {
synchronized (WindowManagerGlobal.class) {
if (sWindowSession == null) {
try {
InputMethodManager imm = InputMethodManager.getInstance(mainLooper);
IWindowManager windowManager = getWindowManagerService();
sWindowSession = windowManager.openSession(
imm.getClient(), imm.getInputContext());
float animatorScale = windowManager.getAnimationScale(2);
ValueAnimator.setDurationScale(animatorScale);
} catch (RemoteException e) {
Log.e(TAG, "Failed to open window session", e);
}
}
return sWindowSession;
}
}
這裡又是一個 Binder 機制的跨進程調用,可以看出 ViewRootImpl 還和 WindowManagerServer 有著密切的聯系,這裡就不討論了。
現在我們來看下 setView 中的調用的一個重要方法。
public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
// ...
// Schedule the first layout -before- adding to the window
// manager, to make sure we do the relayout before receiving
// any other events from the system.
// 重點關注
requestLayout();
if ((mWindowAttributes.inputFeatures
& WindowManager.LayoutParams.INPUT_FEATURE_NO_INPUT_CHANNEL) == 0) {
mInputChannel = new InputChannel();
}
try {
mOrigWindowType = mWindowAttributes.type;
mAttachInfo.mRecomputeGlobalAttributes = true;
collectViewAttributes();
// 這裡調用了 IWindowSession 的 addToDisplay 並且把 W 類型的 mWindow 傳過去
res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
getHostVisibility(), mDisplay.getDisplayId(),
mAttachInfo.mContentInsets, mInputChannel);
} catch (RemoteException e) {
mAdded = false;
mView = null;
mAttachInfo.mRootView = null;
mInputChannel = null;
mFallbackEventHandler.setView(null);
unscheduleTraversals();
setAccessibilityFocus(null, null);
throw new RuntimeException("Adding window failed", e);
} finally {
if (restore) {
attrs.restore();
}
}
// ...
}
現在我們來看看 requestLayout 函數,這裡才是 Activity 的 UI 繪制,進去看看發現其實是一個異步任務中執行了那些繪制任務。
final class TraversalRunnable implements Runnable {
@Override
public void run() {
doTraversal();
}
}
void doTraversal() {
if (mTraversalScheduled) {
mTraversalScheduled = false;
mHandler.getLooper().removeSyncBarrier(mTraversalBarrier);
if (mProfile) {
Debug.startMethodTracing("ViewAncestor");
}
Trace.traceBegin(Trace.TRACE_TAG_VIEW, "performTraversals");
try {
performTraversals();
} finally {
Trace.traceEnd(Trace.TRACE_TAG_VIEW);
}
if (mProfile) {
Debug.stopMethodTracing();
mProfile = false;
}
}
}
這個函數還是比較麻煩的,這裡就給出關鍵點。
private void performTraversals() {
// ...
relayoutResult = relayoutWindow(params, viewVisibility, insetsPending);
// ...
// 一些繪制相關工作
mView.draw(layerCanvas);
// ...
}
private int relayoutWindow(WindowManager.LayoutParams params, int viewVisibility,
boolean insetsPending) throws RemoteException {
float appScale = mAttachInfo.mApplicationScale;
boolean restore = false;
if (params != null && mTranslator != null) {
restore = true;
params.backup();
mTranslator.translateWindowLayout(params);
}
if (params != null) {
if (DBG) Log.d(TAG, "WindowLayout in layoutWindow:" + params);
}
mPendingConfiguration.seq = 0;
if (params != null && mOrigWindowType != params.type) {
// For compatibility with old apps, don't crash here.
if (mTargetSdkVersion < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
Slog.w(TAG, "Window type can not be changed after "
+ "the window is added; ignoring change of " + mView);
params.type = mOrigWindowType;
}
}
int relayoutResult = mWindowSession.relayout(
mWindow, mSeq, params,
(int) (mView.getMeasuredWidth() * appScale + 0.5f),
(int) (mView.getMeasuredHeight() * appScale + 0.5f),
viewVisibility,
insetsPending ? WindowManagerGlobal.RELAYOUT_INSETS_PENDING : 0,
mWinFrame, mPendingContentInsets, mPendingVisibleInsets,
mPendingConfiguration, mSurface);
if (restore) {
params.restore();
}
if (mTranslator != null) {
mTranslator.translateRectInScreenToAppWinFrame(mWinFrame);
mTranslator.translateRectInScreenToAppWindow(mPendingContentInsets);
mTranslator.translateRectInScreenToAppWindow(mPendingVisibleInsets);
}
return relayoutResult;
}
這裡是調用了 IWindowSession 的 relayout 來獲得一個 relayoutResult。至此,Activity 的創建和 UI 的顯示就完成了。
既然是 Activity 的顯示,那麼必然是少不了顯示我們自己設置的 UI。一般我們設置的 UI 都是在 OnCreate 中的 setContentView 中設置,現在我們就來看看這個函數到底做了哪些事。
Activity 中的 setContentView():
public void setContentView(View view) {
getWindow().setContentView(view);
initActionBar();
}
還記得上面所說的 Activity 顯示中有一個 Window 吧,那個 getWindow 就是 PhoneWindow,那麼我們就來看看 PhoneWindow 中的 setContentView。
@Override
public void setContentView(View view) {
setContentView(view, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
}
@Override
public void setContentView(View view, ViewGroup.LayoutParams params) {
if (mContentParent == null) {
installDecor();
} else {
mContentParent.removeAllViews();
}
mContentParent.addView(view, params);
final Callback cb = getCallback();
if (cb != null && !isDestroyed()) {
cb.onContentChanged();
}
}
這裡的 mContentParent 是一個 ViewGroup 類型的,這裡我們可以看到它把我們設置的 view 添加進去了,也就是說繪制 ViewGroup 時也會把它包含的 view 繪制出來。
接下來再來看看 installDecor:
private void installDecor() {
if (mDecor == null) {
mDecor = generateDecor();
mDecor.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
mDecor.setIsRootNamespace(true);
if (!mInvalidatePanelMenuPosted && mInvalidatePanelMenuFeatures != 0) {
mDecor.postOnAnimation(mInvalidatePanelMenuRunnable);
}
}
if (mContentParent == null) {
mContentParent = generateLayout(mDecor);
// Set up decor part of UI to ignore fitsSystemWindows if appropriate
mDecor.makeOptionalFitsSystemWindows();
mTitleView = (TextView) findViewById(com.android.internal.R.id.title);
if (mTitleView != null) {
mTitleView.setLayoutDirection(mDecor.getLayoutDirection());
if ((getLocalFeatures() & (1 << FEATURE_NO_TITLE)) != 0) {
View titleContainer = findViewById(com.android.internal.R.id.title_container);
if (titleContainer != null) {
titleContainer.setVisibility(View.GONE);
} else {
mTitleView.setVisibility(View.GONE);
}
if (mContentParent instanceof FrameLayout) {
((FrameLayout) mContentParent).setForeground(null);
}
} else {
mTitleView.setText(mTitle);
}
} else {
mActionBar = (ActionBarView) findViewById(com.android.internal.R.id.action_bar);
if (mActionBar != null) {
mActionBar.setWindowCallback(getCallback());
if (mActionBar.getTitle() == null) {
mActionBar.setWindowTitle(mTitle);
}
final int localFeatures = getLocalFeatures();
if ((localFeatures & (1 << FEATURE_PROGRESS)) != 0) {
mActionBar.initProgress();
}
if ((localFeatures & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) {
mActionBar.initIndeterminateProgress();
}
boolean splitActionBar = false;
final boolean splitWhenNarrow = (mUiOptions
& ActivityInfo.UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW) != 0;
if (splitWhenNarrow) {
splitActionBar = getContext().getResources().getBoolean(
com.android.internal.R.bool.split_action_bar_is_narrow);
} else {
splitActionBar = getWindowStyle().getBoolean(
com.android.internal.R.styleable.Window_windowSplitActionBar, false);
}
final ActionBarContainer splitView = (ActionBarContainer)
findViewById(com.android.internal.R.id.split_action_bar);
if (splitView != null) {
mActionBar.setSplitView(splitView);
mActionBar.setSplitActionBar(splitActionBar);
mActionBar.setSplitWhenNarrow(splitWhenNarrow);
final ActionBarContextView cab = (ActionBarContextView)
findViewById(com.android.internal.R.id.action_context_bar);
cab.setSplitView(splitView);
cab.setSplitActionBar(splitActionBar);
cab.setSplitWhenNarrow(splitWhenNarrow);
} else if (splitActionBar) {
Log.e(TAG, "Requested split action bar with "
+ "incompatible window decor! Ignoring request.");
}
// Post the panel invalidate for later; avoid application
// onCreateOptionsMenu being called in the middle of onCreate or similar.
mDecor.post(new Runnable() {
public void run() {
// Invalidate if the panel menu hasn't been created before this.
PanelFeatureState st = getPanelState(FEATURE_OPTIONS_PANEL, false);
if (!isDestroyed() && (st == null || st.menu == null)) {
invalidatePanelMenu(FEATURE_ACTION_BAR);
}
}
});
}
}
}
}
從這裡我們可以看出,這個函數主要是創建了一個 DecorView 和 ViewGroup(mContentParent)對象,以及創建標題欄或者是 ActionBar。
再來看看 mContentParent:
protected ViewGroup generateLayout(DecorView decor) {
// ...
mDecor.startChanging();
// layoutResource 是一個資源 ID
View in = mLayoutInflater.inflate(layoutResource, null);
decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
// ID_ANDROID_CONTENT 是 com.android.internal.R.id.content
ViewGroup contentParent = (ViewGroup) findViewById(ID_ANDROID_CONTENT);
if (contentParent == null) {
throw new RuntimeException("Window couldn't find content container view");
}
if ((features & (1 << FEATURE_INDETERMINATE_PROGRESS)) != 0) {
ProgressBar progress = getCircularProgressBar(false);
if (progress != null) {
progress.setIndeterminate(true);
}
}
// ...
return contentParent;
}
這裡的 contentParent 是由 findViewById 獲得,實際是 mDecorView 的一部分。為什麼這麼說?看看下面的代碼就知道了。
public View findViewById(int id) {
return getDecorView().findViewById(id);
}
getDecorView 返回的便是 mDecorView。到現在我們才算是明白圖 1-1:其實我們設置的 View 就是在 DecorView 中,而 DecorView 除了處理我們設置的 View,還處理了標題欄的顯示。
Android 最佳實踐:UI 開發從多螢幕適配到沉浸式體驗
這篇整理 Android 官方 UI 訓練指南的實作要點,從多螢幕適配一路談到自訂 View、向下相容與沉浸式全螢幕,適合當作專案開發時的對照清單。 一、支援多種螢幕尺
讓 Android Activity 變成半透明浮動視窗
有時候,我們希望 Activity 不要以全螢幕形式出現,而是像浮動視窗一樣,置中顯示並帶有半透明背景。這可以透過調整 Window 參數與自訂主題來完成。調整 Acti
Android TextView 進階:顯示支援 HTML 與可點擊連結的豐富文本
本文分享如何在 Android 開發中讓 TextView 顯示豐富的文本內容,供需要的朋友參考。 如下圖所示,可以顯示 HTML 的各種元素控制項,點擊連結還能實現上網
Android 動畫實戰:從螢幕底部滑出的 PopupWindow
本文示範一個常見的互動效果:點擊後,面板從螢幕底部滑出。拍照/選圖、分享選單等情境幾乎都會用到這種設計。 以下仿作一個同類效果: 一、面板版面 take_pho