8.3.2 Dialog的Window創(chuàng)建過程
Dialog的Window的創(chuàng)建過程和Activity類似。
1. 創(chuàng)建Window
Dialog中Window的創(chuàng)建同樣是通過PolicyManager的makeNewWindow方法來完成的,創(chuàng)建后的對象實際上就是PhoneWindow谢揪,這個過程和Activity的Window的創(chuàng)建過程是一致的锣险。
Dialog(Context context, int theme, boolean createContextThemeWrapper) {
if (createContextThemeWrapper) {
if (theme == 0) {
TypedValue outValue = new TypedValue();
context.getTheme().resolveAttribute(com.android.internal.R.attr.dialogTheme,
outValue, true);
theme = outValue.resourceId;
}
mContext = new ContextThemeWrapper(context, theme);
} else {
mContext = context;
}
mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
Window w = PolicyManager.makeNewWindow(mContext);
mWindow = w;
w.setCallback(this);
w.setOnWindowDismissedCallback(this);
w.setWindowManager(mWindowManager, null, null);
w.setGravity(Gravity.CENTER);
mListenersHandler = new ListenersHandler(this);
}
2. 初始化DecorView并將Dialog的視圖添加到DecorView中
這個過程也和Activity的類似牡肉,都是通過Window去添加指定的布局文件。
public void setContentView(int layoutResID) {
mWindow.setContentView(layoutResID);
}
3. 將DecorView添加到Window中并顯示
在Dialog的show方法中,會通過WindowManager將DecorView添加到Window中
mWindowManager.addView(mDecor, l);
mShowing = true;
從上面三個步驟可以發(fā)現(xiàn)篮撑,Dialog的Window創(chuàng)建和Activity的Window創(chuàng)建過程很類似,兩者幾乎沒有什么區(qū)別匆瓜。當Dialog被關閉時赢笨,它會通過WindowManager來移除DecorView:mWindowManager.removeViewImmediate(mDecor)未蝌。
普通的Dialog有一個特殊之處,那就是必須采用Activity的Context茧妒,如果采用Application的Context萧吠,那么就會報錯。
Dialog dialog = new Dialog(this.getApplicationContext());
TextView textView = new TextView(this);
textView.setText("this is toast");
dialog.setContentView(textView);
dialog.show();
上述代碼會報錯桐筏,錯誤信息如下:
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:765)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
at android.app.Dialog.show(Dialog.java:330)
at com.study.wumeng.practice.MainActivity.onCreate(MainActivity.java:20)
at android.app.Activity.performCreate(Activity.java:7009)
at android.app.Activity.performCreate(Activity.java:7000)
是沒有應用token所導致的纸型,而應用token一般只有Activity擁有,所以這里只需要用Activity作為Context來顯示對話框即可梅忌。另外狰腌,系統(tǒng)Window比較特殊,它可以不需要token牧氮,因此在上面的例子中琼腔,只需要指定對話框的Window為系統(tǒng)類型就可以正常彈出對話框。在本章一開始講到踱葛,WindowManager.LayoutParams中的type表示W(wǎng)indow的類型丹莲,而系統(tǒng)Window的層級范圍是2000~2999,這些層級范圍就對應著type參數(shù)尸诽。系統(tǒng)WIndow的層級有很多值甥材,對于本例來說,可以選擇用TYPE_SYSTEM_OVERLAY來指定對話框的Window類型為系統(tǒng)Window性含。
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY);
然后別忘了在AndroidManifest文件中聲明權限從而可以使用系統(tǒng)Window擂达。
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>