add View by using WindowManager, but can back key press

前端 未结 3 1909
北荒
北荒 2021-01-03 07:08

I\'ve add a View, by using WindowManager.

It shows properly what I wanted to do,

but I have a problem. this is the problem.

  • back key press does
3条回答
  •  抹茶落季
    2021-01-03 07:31

    You can do like this:

    Add a Float window by WindowManager, for example, add a view to screen bottom:

    WindowManager windowManager = (WindowManager) getActivity().getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    
    WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(); 
    layoutParams.width   = WindowManager.LayoutParams.WRAP_CONTENT;
    layoutParams.height  = WindowManager.LayoutParams.WRAP_CONTENT;
    layoutParams.type= WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    layoutParams.flags   =  WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
    layoutParams.format  = PixelFormat.TRANSLUCENT;
    layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
    layoutParams.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    
    FrameLayout view = new FrameLayout(getActivity()) {
                @Override
                public boolean dispatchKeyEvent(KeyEvent event) {
                    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
                        // do you code
                        return true;
                    }
                    return super.dispatchKeyEvent(event);
                }
            };
    windowManager.addView(view, layoutParams); 
    

提交回复
热议问题