如何单击工具栏后面的视图? [英] How to click views behind a Toolbar?

本文介绍了如何单击工具栏后面的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工具栏,背景透明/半透明,可以覆盖内容.因此,在工具栏后面可以显示可单击的视图.问题是无法通过工具栏单击它们,因为工具栏正在捕获单击事件.

I have a toolbar with a transparent/translucent background that overlays the content. So, behind the toolbar, views can appear that are clickable. The problem is that they can not be clicked through the toolbar because the toolbar is catching the click event.

我尝试为工具栏设置android:clickable="false"android:focusable="false"android:focusableInTouchMode="false",但是没有效果.如何通过工具栏将点击发送到基础视图?

I tried setting android:clickable="false", android:focusable="false" and android:focusableInTouchMode="false" for the toolbar, but it has no effect. How can I send a click through the toolbar to the underlying view?

推荐答案

看看Toolbar的实现.不管clickable属性如何,它都会吃触摸事件.

Take a look at the implementation of Toolbar. It eats touch events, regardless of the clickable attribute.

@Override
public boolean onTouchEvent(MotionEvent ev) {
    // Toolbars always eat touch events, but should still respect the touch event dispatch
    // contract. If the normal View implementation doesn't want the events, we'll just silently
    // eat the rest of the gesture without reporting the events to the default implementation
    // since that's what it expects.

    final int action = MotionEventCompat.getActionMasked(ev);
    if (action == MotionEvent.ACTION_DOWN) {
        mEatingTouch = false;
    }

    if (!mEatingTouch) {
        final boolean handled = super.onTouchEvent(ev);
        if (action == MotionEvent.ACTION_DOWN && !handled) {
            mEatingTouch = true;
        }
    }

    if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
        mEatingTouch = false;
    }

    return true;
}

解决方案是从Toolbar扩展并覆盖onTouchEvent.

The solution is to extend from Toolbar and override onTouchEvent.

public class NonClickableToolbar extends Toolbar {

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return false;
    }
}

这篇关于如何单击工具栏后面的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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