【发布时间】:2025-12-10 04:15:01
【问题描述】:
我使用了底部应用栏,问题是当我在菜单中使用 app:actionLayout 为我的菜单使用自定义布局时,单击对这些菜单不起作用,即底部应用栏的 setOnMenuItemClickListener 不起作用。? 不使用 actionLayout 时它工作正常。
BottomAppBar bottomAppBar;
bottomAppBar = findViewById(R.id.bar);
bottomAppBar.replaceMenu(R.menu.announcement_menu);
bottomAppBar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_all:
Log.d("Announcement", "All clicked");
break;
case R.id.action_rec:
Log.d("Announcement", "Received clicked");
break;
case R.id.action_sen:
Log.d("Announcement", "Sent clicked");
break;
}
return false;
}
});
还有菜单文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_all"
app:actionLayout="@layout/custom_menu_row_all"
android:title="All"
app:showAsAction="always" />
<item
android:id="@+id/action_rec"
app:actionLayout="@layout/custom_menu_row_received"
android:title="Received"
app:showAsAction="always" />
<item
android:id="@+id/action_sen"
app:actionLayout="@layout/custom_menu_row_sent"
android:title="Sent"
app:showAsAction="always" />
</menu>
【问题讨论】:
-
也许您需要为每个项目设置点击侦听器,例如:R.id.action_all.setonclicklistener(this)。试试这个!
标签: android android-menu android-bottomappbar