本文介绍了如何创建一个覆盖层,以阻止触摸事件进入其下方的UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了具有半透明背景的framelayout层来创建叠加层.但是,此叠加层不会阻止触摸事件与其下的视图进行交互.应该如何创建一个覆盖所有触摸事件的叠加层?

I used a layer of framelayout with a semi-translucent background to create an overlay. But this overlay doesn't block touch events to interact with the views below it.How should create an overlay that blocks all touch events?

推荐答案

以下代码将在所有内容之上添加覆盖:

Following code will add overlay on top of everything :

View v1 = new View(this);    
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
 1000,
 50,
 WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
 WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
 PixelFormat.OPAQUE);

params.gravity = Gravity.BOTTOM;
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
wm.addView(v1, params);

要阻止触摸事件,您必须更改标志,否则以下代码将起作用:

to block the touch event either you have to change the flag or below code will work:

protected boolean onTouchEvent (MotionEvent me) {
    return true;
}

对于v1,您需要导入:

For v1 you would do an import:

import android.view.View.OnTouchListener;

然后设置onTouchListener:

Then set the onTouchListener:

v1.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }
})

这篇关于如何创建一个覆盖层,以阻止触摸事件进入其下方的UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 20:07