窗口管理器视图不可见

时间:2020-04-07 07:41:53

标签: android

我正在使用窗口管理器显示服务中的弹出窗口,但是我的弹出视图不可见。它是空白的。有人知道为什么会这样吗?

我也在清单中写了android.permission.SYSTEM_ALERT_WINDOW许可。我的代码看起来像这样。

    int LAYOUT_FLAG;
    if (Build.VERSION.SDK_INT >= 26) {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    } else if (Build.VERSION.SDK_INT >= 22) {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_TOAST;
    } else {
        LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
    }

    WindowManager.LayoutParams p = new WindowManager.LayoutParams(
            // Shrink the window to wrap the content rather than filling the screen
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            // Display it on top of other application windows, but only for the current user
            LAYOUT_FLAG,
            // Don't let it grab the input focus
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            // Make the underlying application window visible through any transparent parts
            PixelFormat.TRANSLUCENT);

    // Define the position of the window within the screen
    p.gravity = Gravity.TOP | Gravity.RIGHT;
    p.x = 0;
    p.y = 100;

    windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);

    LayoutInflater layoutInflater =
            (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
    binding = DataBindingUtil.inflate(layoutInflater, R.layout.activity_lock, null, false);

    windowManager.addView(binding.getRoot(), p);

1 个答案:

答案 0 :(得分:0)

它需要用户的“透支其他应用”权限。请在设置中搜索此权限,并允许其用于您的应用。