如何创建一个覆盖层来阻止它下面的 UI 的触摸事件? [英] How to create an overlay that blocks touch events to UI below it?

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

问题描述

我使用了一层带有半透明背景的框架布局来创建叠加层.但是这个叠加层不会阻止触摸事件与其下方的视图进行交互.应如何创建阻止所有触摸事件的叠加层?

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 的触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆