简体   繁体   English

使用actionLayout属性时,为什么不能单击我的操作栏菜单项?

[英]Why can't I click my action bar menu item when using actionLayout property?

Why does my menu item become unclickable if I use the actionLayout property in menu_main.xml? 如果在menu_main.xml中使用actionLayout属性,为什么菜单项变得不可点击? onOptionsItemSelected() is never called. 从不调用onOptionsItemSelected()。 How can I correct this issue? 我该如何解决这个问题?

menu_main.xml: menu_main.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/menu_mail"
        android:title="@string/menuMailTitle"
        android:actionLayout="@layout/icon_mail"
        android:icon="@drawable/icons_mail"
        android:showAsAction="always" />

</menu>

onCreateOptionsMenu(): onCreateOptionsMenu():

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);
    MenuItemCompat.setShowAsAction(menu.findItem(R.id.menu_mail), MenuItem.SHOW_AS_ACTION_ALWAYS);
    return true;
}

It should be used like so: 应该这样使用:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_mail, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.icons_mail:
            break;
        default:
            return super.onOptionsItemSelected(item);
    }
    return true;
}

Change your menu from: 从以下菜单中更改:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_mail"
        android:title="@string/menuMailTitle"
        android:actionLayout="@layout/icon_mail"
        android:icon="@drawable/icons_mail"
        android:showAsAction="always" />

</menu>

to

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/menu_mail"
        android:title="@string/menuMailTitle"
        app:actionLayout="@layout/icon_mail"
        android:icon="@drawable/icons_mail"
        app:showAsAction="always" />

</menu>

Notice the use of the "app"lication specific namespace. 注意使用“ app”许可特定名称空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 OnOptionsItemSelected 没有为具有自定义 actionLayout 的操作栏菜单项调用 - OnOptionsItemSelected not being called for Action Bar Menu Item with custom actionLayout Android操作栏菜单项,actionLayout无法正常工作 - Android action bar menu item with actionLayout not working properly 带有 actionLayout 的 NavigationView 菜单项。 如何为每个项目设置这些动作布局的属性? - NavigationView menu items with actionLayout. How can I set those action layout's attribute for each item? 当我单击上下文操作栏上的项目时如何插入菜单 - How to insert a menu when i click in a item on the contextual action bar 操作栏子菜单不与actionLayout一起使用 - Action bar sub menu not working with actionLayout 菜单中的 onOptionsItemSelected 对于使用 actionLayout 的项目不可点击 - onOptionsItemSelected in Menu is not clickable for item using actionLayout 如何从操作栏项创建下拉选项菜单? - How can I create a dropdown options menu from my action bar item? 当选择了操作栏中的其他菜单项时,需要单击“溢出菜单项”两次 - Need to click Overflow Menu Item twice when other menu item in Action bar is selected 如何在操作栏中将菜单项居中? - How can I center a menu item in an action bar? 如何使用actionbarsherlock将菜单项添加到操作栏? - How can I add a menu item to the action bar with actionbarsherlock?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM