【问题标题】:Android overlay layout on top of all windows that receives touches接收触摸的所有窗口顶部的 Android 覆盖布局
【发布时间】:2012-09-06 01:50:58
【问题描述】:

我使用以下代码创建了一个显示在所有应用程序和窗口之上的视图:

        //These three are our main components.
    WindowManager wm;
    LinearLayout ll;
    WindowManager.LayoutParams ll_lp;

    //Just a sample layout parameters.
    wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    ll_lp = new WindowManager.LayoutParams();
    ll_lp.format = PixelFormat.TRANSLUCENT;
    ll_lp.height = WindowManager.LayoutParams.FILL_PARENT;
    ll_lp.width = WindowManager.LayoutParams.FILL_PARENT;
    ll_lp.gravity = Gravity.CLIP_HORIZONTAL | Gravity.TOP;

    //This one is necessary.
    ll_lp.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;

    //Play around with these two.
    ll_lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
    ll_lp.flags = ll_lp.flags | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

    //This is our main layout.
    ll = new LinearLayout(this);
    ll.setBackgroundColor(android.graphics.Color.argb(50, 255, 255, 255));
    ll.setHapticFeedbackEnabled(true);
    ll.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Toast.makeText(getApplicationContext(), "TOUCHED", Toast.LENGTH_SHORT).show();
            return false;
        }
        });

    //And finally we add what we created to the screen.
    wm.addView(ll, ll_lp);

因为设置了 FLAG_NOT_TOUCHABLE,它只显示视图但不接收任何触摸事件。视图后面的应用程序接收所有触摸事件。 但是,如果我不设置标志,那么只有视图会接收到触摸,导致其后面的应用程序不会接收到任何触摸。

有没有办法让视图和它背后的应用程序都接收触摸?我试过返回 false 但还是一样。

任何帮助将不胜感激!

【问题讨论】:

  • 你搞定了吗?

标签: android events layout view touch


【解决方案1】:

如果我没记错的话,后台的视图没有接收到触摸事件,因为它被系统过滤掉以防止“点击劫持”漏洞。

您可以通过使用View.setFilterTouchesWhenObscured(Boolean) 方法关闭后台视图的触摸事件过滤来绕过此系统功能。

【讨论】:

  • 我可以为覆盖视图本身设置任何属性以阻止它过滤触摸吗?因为我无法控制它背后的观点。谢谢
  • 对不起,我不认为你可以。您只能在“您的”视图被遮挡时控制触摸事件的过滤。
猜你喜欢
  • 2018-06-25
  • 1970-01-01
  • 1970-01-01
  • 2010-11-06
  • 2013-12-13
  • 1970-01-01
  • 1970-01-01
  • 2018-07-31
  • 1970-01-01
相关资源
最近更新 更多