i am using actionbar in android but its not showing the overflowbar.i am trying as tutorial but its not working althoough i did the same whats wrong?i just want home to display and the others on overflow.here is the menue codei am using actionbar in android but its not showing the overflowbar.i am trying as tutorial but its not working althoough i did the same whats wrong?i just want home to display and the others on overflow.here is the menue code

<item android:id="@+id/Homebar"
    android:title="Home"
    android:orderInCategory="1"
    android:showAsAction="ifRoom"
    />
<item android:id="@+id/infobar"
    android:title="Info"
    android:orderInCategory="2"
    android:showAsAction="ifRoom"
    />
<item android:id="@+id/servicebar"
    android:title="Services"
    android:orderInCategory="2"
    android:showAsAction="ifRoom"
    />
<item android:id="@+id/onlinetoolsbar"
    android:title="Online tools"
    android:orderInCategory="2"
    android:showAsAction="ifRoom"
    />
<item android:id="@+id/mediacenterbar"
    android:title="Media center"
    android:orderInCategory="2"
    android:showAsAction="ifRoom"
    />

有帮助吗?

解决方案

You are probably using a device with a dedicated menu button, or an emulator with a dedicated menu button. Prior to Android 4.4, Android won't show an overflow button if the devices has a dedicated menu button. If you press the menu button, the overflow menu should appear.

Google changed this behaviour in Android 4.4 Kitkat, and now all devices will show the overflow button. If you're using the emulator, try testing on the Android 4.4 emulator. If you're testing on a real device with a dedicated menu button, use that button to 'summon' the overflow menu.

EDIT: To get a custom submenu/overflow menu, you have to create a seperate menu inside a menuitem. Like this:

 <item android:id="@+id/Homebar"
    android:title="Home"
    android:orderInCategory="1"
    android:showAsAction="ifRoom"
    >
    <menu>
     <item android:id="@+id/infobar"
         android:title="Info"
        android:orderInCategory="2"
         android:showAsAction="ifRoom"
         />
     <item android:id="@+id/servicebar"
         android:title="Services"
         android:orderInCategory="2"
         android:showAsAction="ifRoom"
         />
     <item android:id="@+id/onlinetoolsbar"
         android:title="Online tools"
         android:orderInCategory="2"
         android:showAsAction="ifRoom"
         />
     <item android:id="@+id/mediacenterbar"
         android:title="Media center"
         android:orderInCategory="2"
         android:showAsAction="ifRoom"
         />
     </menu>  
</item>

其他提示

I hope below code useful for the most of all devices for showing the action bar with overflow menu and uses:

This below way is easy for showing Action Bar overflow menu in most of all the device, which you can use > 3.0 versions.

I checked this code is working properly in Android 4.0, 4.1, 4.2, 4.3 versions.

MainActivity.java

package com.example.actionbaroverflowmenu;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.WindowManager;

public class MainActivity extends Activity {
private Menu mainMenu;

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.

    super.onCreateOptionsMenu(menu);

    getMenuInflater().inflate(R.menu.main, menu);

    mainMenu = menu;
    return true;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub

    if(event.getAction() == KeyEvent.ACTION_DOWN){
        switch (keyCode) {
        case KeyEvent.KEYCODE_MENU:
            mainMenu.performIdentifierAction(R.id.action_overflow, 0);
            return true;
        }
    }

    return super.onKeyDown(keyCode, event);
}

}

menu/main.xml

<item
    android:id="@+id/action_locate"
    android:icon="@drawable/ic_action_locate"
    android:orderInCategory="200"
    android:showAsAction="always"/>
<item
    android:id="@+id/action_refresh"
    android:icon="@drawable/ic_action_refresh"
    android:orderInCategory="300"
    android:showAsAction="always"/>
<item
    android:id="@+id/action_overflow"
    android:icon="@drawable/ic_action_overflow"
    android:menuCategory="container"
    android:orderInCategory="400"
    android:showAsAction="always"
    android:visible="true">
    <menu>
        <item
            android:id="@+id/action_refresh"
            android:icon="@drawable/ic_action_refresh"
            android:menuCategory="container"
            android:orderInCategory="100"
            android:showAsAction="always"
            android:title="Refresh"
            android:visible="true"/>
        <item
            android:id="@+id/action_search"
            android:icon="@drawable/ic_action_search"
            android:menuCategory="container"
            android:orderInCategory="200"
            android:showAsAction="always"
            android:title="Search"
            android:visible="true"/>
        <item
            android:id="@+id/action_settings"
            android:icon="@drawable/ic_action_settings"
            android:orderInCategory="300"
            android:showAsAction="always"
            android:title="@string/action_settings"/>

        <item
            android:id="@+id/action_help"
            android:icon="@drawable/ic_action_help"
            android:orderInCategory="400"
            android:showAsAction="always"
            android:title="Help"/>

        <item
            android:id="@+id/action_about"
            android:icon="@drawable/ic_action_about"
            android:orderInCategory="500"
            android:showAsAction="always"
            android:title="About"/>

    </menu>
</item>

Download Action Bar Icons Pack From below Links: Download icon pack

You have to put icons in drawable folder for using in menu/main.xml file

Read this from the doc :

Action overflow

The action overflow in the action bar provides access to your app's less frequently used actions. The overflow icon ONLY APPEARS on phones that have no menu hardware keys. Phones with menu keys display the action overflow when the user presses the key.`

So this is not a strange behavior of your app, this is android's normal behavior, depending on what you put in showAsAction attribute in menu.xml. It depends also on the screen size and orientation.

BTW, some poeple use a hack to force the overflow menu to be shown, but as i said it's a hack, so not good for your app, and will lead you to eventual bugs in other devices / versions. Have a look here to better understand.

If you really want it on your app without using the hack, check this solution.

Only those items are displayed in overflow menu whose android:showAsAction is set to "never". You have set it to "ifRoom" for every item.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top