简体   繁体   English

如何使覆盖窗口可触摸

[英]how to make overlay window touchable

i'm making a feature in my app that display an overlay window with text on the screen, when it appears then i try to touch that window it actually touches behind that window i need to make that window touchable , which params should i use ? 我正在我的应用程序中创建一个功能,该功能在屏幕上显示带有文本的覆盖窗口,当它出现时,我尝试触摸该窗口,它实际上在该窗口后面触摸,我需要使该窗口可触摸,我应该使用哪些参数? here is my code 这是我的代码

        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);

I finally found an answer. 我终于找到了答案。

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);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM