AlertDialog 是非阻塞式对话框;而PopupWindow 是阻塞式对话框。
AlertDialog 弹出时,后台还可以做事情;PopupWindow 弹出时,程序会等待,在PopupWindow 退出前,程序一直等待,只有当我们调用了 dismiss() 方法的后,PopupWindow 退出,程序才会向下执行。我们在写程序的过程中可以根据自己的需要选择使用 Popupwindow 或者是 Dialog.
区别:
PopupWindow 没有新建,而是通过 WMS 将 View 加到 DecorView;
Dialog 是新建了一个 window (PhoneWindow),相当于走了一遍 Activity 中创建 window 的流程。
1.AlertDialog
Dialog(@NonNull Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) {
mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
// 创建一个phonewindow显示界面
final Window w = new PhoneWindow(mContext);
mWindow = w;
w.setCallback(this);
}
2.popwindow
不管是showAtLocation还是showAsDropDown,终会调用invokePopup
使用界面原本的decorview, 使用mwindowManager添加到窗口里。
private void invokePopup(WindowManager.LayoutParams p) {
if (mContext != null) {
p.packageName = mContext.getPackageName();
}
final PopupDecorView decorView = mDecorView;
decorView.setFitsSystemWindows(mLayoutInsetDecor);
setLayoutDirectionFromAnchor();
mWindowManager.addView(decorView, p);
if (mEnterTransition != null) {
decorView.requestEnterTransition(mEnterTransition);
}
}