如果我想在屏幕上接收所有触摸事件,但仍然能够与管理器后面的视图进行交互,我会使用什么WindowManager.LayoutParams?

时间:2017-10-04 00:13:18

标签: android android-windowmanager

我目前正在使用此实现

LinearLayout touchLayout = new LinearLayout(this);
        // set layout
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        touchLayout.setLayoutParams(lp);
        // set on touch listener
        touchLayout.setOnTouchListener(this);
        touchLayout.setFocusableInTouchMode(false);
        touchLayout.setFocusable(false);
        touchLayout.setClickable(false);
        // fetch window manager object
        WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        // set layout parameter of window manager
        WindowManager.LayoutParams mParams = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.MATCH_PARENT, // width is equal to full screen
                WindowManager.LayoutParams.MATCH_PARENT, // height is equal to full screen
                WindowManager.LayoutParams.TYPE_SYSTEM_ALERT , //
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, // this window won't ever get key input focus
                PixelFormat.TRANSLUCENT);
        mParams.gravity = Gravity.LEFT | Gravity.TOP;
        mWindowManager.addView(touchLayout, mParams);

唯一的问题是用户无法与我放置的LinearLayout后面的任何内容进行交互。

1 个答案:

答案 0 :(得分:0)

只需在活动窗口或XML文件的根元素上设置touchlistener,然后根据需要决定如何/在何处分配触摸