【发布时间】: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