【问题标题】:Android AppCompat Toolbar does not respond to touching action itemsAndroid AppCompat 工具栏不响应触摸操作项
【发布时间】:2015-05-06 21:31:04
【问题描述】:

我正在关注 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);

我将此 sn-p 代码放在 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" />

Toolbar 是从菜单布局 xml 文件中扩展而来的。它由一个带有单个“项目”元素的菜单元素组成:

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

【问题讨论】:

  • 将工具栏放在布局的最后,这样它就可以覆盖所有内容。相邻 ListView 的 padding 是 ListView 的一部分,并消耗触摸事件。
  • @EugenPechanec 你是对的。这样就解决了问题。谢谢!

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


【解决方案1】:

ListView 绘制在 Toolbar 视图的顶部,因此该视图将获取触摸事件。您应该将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,因为在这里更方便。它做你想做的事。将一个视图与另一个视图对齐,而不是在另一个视图之上。

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

【讨论】:

  • 我没有意识到 LinearyLayout 在这里更方便。这是有道理的,而且效果很好。谢谢。
猜你喜欢
  • 1970-01-01
  • 2014-03-19
  • 2014-12-14
  • 1970-01-01
  • 2015-02-04
  • 2014-12-14
  • 1970-01-01
  • 2016-03-06
  • 1970-01-01
相关资源
最近更新 更多