其他分享
首页 > 其他分享> > android – 如何点击工具栏后面的视图?

android – 如何点击工具栏后面的视图?

作者:互联网

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

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

解决方法:

看一下Toolbar的实现.无论可点击属性如何,它都会触发触摸事件.

@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;
}

解决方案是从工具栏扩展并覆盖onTouchEvent.

public class NonClickableToolbar extends Toolbar {

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

标签:android,android-toolbar,material-design,android-support-library,android-actionba
来源: https://codeday.me/bug/20191007/1869078.html