Android : Place non-clickable view above all application windows but below notification bar

荒凉一梦 提交于 2020-01-17 14:23:11

问题


Sorry for the long title. I would basically like to add a SurfaceView as a window that appears above all windows but not above the notification bar (even when dragged down). The code I currently have is:

WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                PixelFormat.TRANSLUCENT);

This works perfectly except it places the view above the notification bar. If I instead use TYPE_SYSTEM_ALERT, I can achieve the desired Z-order, but then my application will not respond to user input. I then added flag FLAG_NOT_TOUCHABLE, which then allows the user to interact with application components as desired. However, then the back button and the task manager button on the phone do not respond.

Is there anyway to achieve the behavior I'm looking for? I hope I made it clear enough, it's a bit difficult to describe in words what I would like. Thanks!


回答1:


Solved!

WindowManager.LayoutParams params = new WindowManager.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
                WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
                        WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
                        WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                        WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                PixelFormat.TRANSLUCENT);

TYPE_SYSTEM_ALERT ensures that the system notification bar remains above my view. FLAG_NOT_TOUCH_MODAL allows touch events to be sent to the windows behind the view; and FLAG_NOT_TOUCHABLE and FLAG_NOT_FOCUSABLE ensure the view doesn't receive touch or key events.



来源:https://stackoverflow.com/questions/37419671/android-place-non-clickable-view-above-all-application-windows-but-below-notif

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!