首页 > 解决方案 > 当我使用 TYPE_APPLICATION_OVERLAY 时,对话框不可见

问题描述

我在其他应用程序之上显示一个对话框。它一直有效,直到我使用WindowManager.LayoutParams.TYPE_SYSTEM_ALERT

当我将目标版本更改为 26 时,我不应该使用 TYPE_SYSTEM_ALERT,所以我使用了WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY

之后我的对话框不可见。我还需要做什么?任何建议。

参考链接: https ://developer.android.com/about/versions/oreo/android-8.0-changes#cwt

代码:

    final Dialog dialog = new Dialog(getApplicationContext());

    dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.permission);
    dialog.setCancelable(true);

    dialog.show();

标签: androidandroid-8.0-oreoandroid-accessibilityandroid-overlay

解决方案


对于 oreo 及以上设备我们必须使用 TYPE_APPLICATION_OVERLAY,对于以下我们必须使用 TYPE_SYSTEM_ALERT

          final Dialog dialog = new Dialog(getApplicationContext());
          if(oreoAndAbove) {
              dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
          } else {
              dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
          }
          dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
          dialog.setContentView(R.layout.permission);
          dialog.setCancelable(true);

          dialog.show();

推荐阅读