Android AppCompat 工具栏不响应触摸操作项

标签 android android-layout android-support-library android-actionbar-compat android-toolbar

我正在关注 these instructions在我的 Android 应用程序中实现 AppCompat Toolbar。我使用的是 AppCompat 版本 7:22.0.1 此外,除了 actionBar 之外,我还使用了这个工具栏(它没有替换 ActionBar,正如我在许多其他示例中看到的那样)。

工具栏出现了,它甚至显示了我指定的图标,但是当我触摸该图标时它没有响应。有没有人对 AppCompat Toolbar 有类似的问题?关于下一步尝试什么有什么建议吗?

这是我在 MainActivity.Java 中的 browse() 方法的代码:

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);

// Set an OnMenuItemClickListener to handle menu item clicks
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        // Handle the menu item
        switch (item.getItemId()) {
            case R.id.browse_back_button:
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
                restart(view);
            default:
                return false;
        }
    }
});

// Inflate a menu to be displayed in the toolbar
toolbar.inflateMenu(R.menu.toolbar_menu);

我将这段代码放在 XML 布局文件中,作为 RelativeLayout 元素中的第一项。

<android.support.v7.widget.Toolbar
    android:id="@+id/my_awesome_toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="#FFFF00" />

工具栏 是从菜单布局 xml 文件扩展而来的。它由一个带有单个“item”元素的菜单元素组成:

<item android:title="Back"
    android:id="@+id/browse_back_button"
    android:icon="@drawable/ic_action_back"
    app:showAsAction="always" />

最佳答案

ListView 绘制在 Toolbar View 之上,因此该 View 将获取触摸事件。您应该将 Toolbar 作为布局中的最后一个元素,或者您应该使用垂直 LinearLayout,或者您应该使用 android:layout_below

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

  <ListView
      android:id="@+id/listViewBrowse"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="?attr/actionBarSize" />

  <android.support.v7.widget.Toolbar
      android:id="@+id/my_awesome_toolbar"
      android:layout_height="wrap_content"
      android:layout_width="match_parent"
      android:minHeight="?attr/actionBarSize"
      android:background="#FFFF00" />

</RelativeLayout>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    android:orientation="vertical">

  <android.support.v7.widget.Toolbar
      android:id="@+id/my_awesome_toolbar"
      android:layout_height="wrap_content"
      android:layout_width="match_parent"
      android:minHeight="?attr/actionBarSize"
      android:background="#FFFF00" />

  <ListView
      android:id="@+id/listViewBrowse"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      />

</LinearLayout>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <android.support.v7.widget.Toolbar
       android:id="@+id/my_awesome_toolbar"
       android:layout_height="wrap_content"
       android:layout_width="match_parent"
       android:minHeight="?attr/actionBarSize"
       android:background="#FFFF00" />

    <ListView
        android:id="@+id/listViewBrowse"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:layout_below="@id/my_awesome_toolbar" />

</RelativeLayout>

我会在这里使用 LinearLayout,因为它在这里更方便。它做你想做的事。将一个 View 与另一个 View 对齐,而不是在另一个 View 之上。

(编辑:我看到这已经被评论了,不明白为什么。因为这是对问题的回答。)

关于Android AppCompat 工具栏不响应触摸操作项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30087932/

相关文章:

android - DialogFragment 导致 IllegalStateException

android - 如何在 Android 中将 Imageview 置于 Listview 下方?

android - 在哪里可以下载Google的android程序包? (不是应用程序)

android - getSupportLoaderManager() 在不同 fragment 中使用相同的 ID?

android - 如何使android的数字编辑文本类似于DatePickerDialog上的向上/向下按钮?

android - 如何实现线性布局以显示两个 TextView ,一个在右边,一个在左边?

android - 为什么 Android 不让您知道 ListView 的滚动 y 位置?

即使在 AppCompat 23.1.0 中设置填充 0,Android SeekBar 也不能全宽

安卓 "unable to load sample (null)"

java - 为什么我们要从存储库返回实时数据? (MVVM 安卓)