如何使叠加窗口可触摸

时间:2018-01-16 20:02:33

标签: android overlay window-managers system-alert-window

我在我的应用中制作了一个功能,在屏幕上显示带有文字的覆盖窗口,当它出现时,我尝试触摸它实际触及该窗口后面的那个窗口 我需要让那个窗口可以触摸,我应该使用哪个params? 这是我的代码

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        LayoutInflater screenNotificationInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        layOverTopviewNotification =  screenNotificationInflater.inflate(R.layout.layout_notification, null);
        TextView layOverText = (TextView) layOverTopviewNotification.findViewById(R.id.notification_layout);
        layOverText.setText(athkarString[completed]);

        params = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);
        params.x = 0;
        params.y = 0;
        params.gravity = Gravity.TOP;
        windowManager.addView(layOverTopviewNotification, params);

2 个答案:

答案 0 :(得分:1)

我终于找到了答案。

windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

inflater = LayoutInflater.from(PlayAthkarService.this);

textView = new TextView(PlayAthkarService.this);
textView.setBackgroundResource(R.drawable.floating_window);
textView.setTextSize(20);
textView.setText(athkarString[completed]);
textView.setTextColor(getResources().getColor(R.color.colorPrimaryDark));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    params = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT
        , WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
        PixelFormat.OPAQUE);
}else {
    params = new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
            , WindowManager.LayoutParams.TYPE_PRIORITY_PHONE,
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
            PixelFormat.OPAQUE);
}

params.gravity = Gravity.TOP | Gravity.RIGHT ;
params.x = 0;
params.y = 50;

windowManager.addView(textView, params);

答案 1 :(得分:-1)

也许您可以尝试将textview设置为touchListener并使textview全屏显示