简体   繁体   中英

ActionBar item does not shown in Option Menu

I can't show items inside the "Menu Item" when those items are already displayed on the Action bar.

This is my onCreateOptionsMenu method:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.opt_menu_main, menu);
    return super.onCreateOptionsMenu(menu);
}

This is my opt_menu_main layout:

<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item
    android:id="@+id/menu_act_settings"
    android:title="Settings" 
    android:icon="@drawable/ic_action_settings"
    android:showAsAction="ifRoom"/>
</menu>

I'm not using support library (my target API is 17).

If I click on the button Menu it is always empty because the item is already displayed up on the ActionBar. I've tried to add more items but when the items goes up on the ActionBar, they are not displayed inside the options menu.

I've also tried with showAsAction="ifRoom|withText" but doesn't work.

I think this behavior is correct, but is it possible to show the same item both in Menu and Action Bar at the same time?

Thanks

Try this:

app:showAsAction="always"

but don't forget to add this line at the begin of definition:

xmlns:app="http://schemas.android.com/apk/res-auto"

The Id's for menu items must be unique. The best option is to create another menu item with another Id and set its showAsAction to "never" to force it always into the overflow menu. But their is a chance if you are also displaying other primary options it might force both into the overflow in which case you will see two menu items with the same title.

For the moment I solved like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item
    android:id="@+id/menu_act_settings"
    android:title="Settings" 
    android:icon="@drawable/ic_action_settings"
    android:showAsAction="always"/>
    <item
    android:id="@+id/menu_settings"
    android:title="Settings" 
    android:showAsAction="never"/>
</menu>

So in the onOptionsItemSelected method :

public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
        case R.id.menu_add_schedule:
        case R.id.menu_act_add_schedule:
                //some code
            break; 
        }
    return true; 
}

I don't think this is a correct solution, because on some device where there is not enough space to add this and other items up on the ActionBar, since they are defined as showAsAction="always", they will be overlapped on the ActionBar Title, so it can create some layout problems.

The behavior that I want is to show always the items on the option menu, and show it also in the action bar if there is enough space.

This behavior can not be obtained with showAsAction="always" .

It would be great if someone could find a better solution.

Thanks anyway.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM