我用以下代码创建了一个显示在所有应用程序和窗口顶部的视图:
//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,但仍然是一样的。
任何帮助都将不胜感激!
发布于 2012-09-13 10:35:09
如果我没弄错的话,背景中的视图没有接收到触摸事件,因为它被系统过滤掉了,以防止“点击劫持”漏洞。
通过使用View.setFilterTouchesWhenObscured(Boolean)方法关闭后台视图的触摸事件过滤,您可能能够绕过此系统功能。
https://stackoverflow.com/questions/12398402
复制相似问题