How do I show Overflow Menu Items to Action Bar in

2019-01-10 22:48发布

问题:

I am writing an Android App in which i am trying to show Overflow Menu Items to ActionBar

using this great tutorial link: http://wptrafficanalyzer.in/blog/adding-action-items-and-overflow-menu-items-to-action-bar-in-android/

Problem:

Not getting Overflow Menu Items (Icon)

Please see below Screen Shot for more clarity:

Manifest.xml:

<uses-sdk android:minSdkVersion="14" />

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" 
        android:uiOptions="splitActionBarWhenNarrow"              
        >

items.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
    android:id="@+id/phone"
    android:title="@string/phone"
    android:icon="@drawable/phone"
    android:showAsAction="ifRoom|withText"
/>

<item
    android:id="@+id/computer"
    android:title="@string/computer"
    android:icon="@drawable/computer"
    android:showAsAction="ifRoom|withText"
/>

<item
    android:id="@+id/gamepad"
    android:title="@string/gamepad"
    android:icon="@drawable/gamepad"
    android:showAsAction="ifRoom|withText"
/>

<item
    android:id="@+id/camera"
    android:title="@string/camera"
    android:icon="@drawable/camera"
    android:showAsAction="ifRoom|withText"
/>

<item
    android:id="@+id/video"
    android:title="@string/video"
    android:icon="@drawable/video"
    android:showAsAction="ifRoom|withText"
/>

<item
    android:id="@+id/email"
    android:title="@string/email"
    android:icon="@drawable/email"
    android:showAsAction="ifRoom|withText"
/>
 </menu>

I am using this tutorial, and trying to make Figure 6 : Action items and Overflow menu in Split Action Bar

Please help me to show Overflow Menu Items (ICON) to ActionBar

Now whenever i do click on Menu Button in emulator, then i am getting rest Menu Items....

回答1:

To show three dot icon, to Action Bar, just use below method in your OnCreate():

   private void getOverflowMenu() {

    try {
       ViewConfiguration config = ViewConfiguration.get(this);
       Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
       if(menuKeyField != null) {
           menuKeyField.setAccessible(true);
           menuKeyField.setBoolean(config, false);
       }
   } catch (Exception e) {
       e.printStackTrace();
   }
 }


回答2:

Just to say that If your device has a menu-button, the overflow-icon won't show, on the newer phones the overflow button will show. I would not reccomend ASMUIRTI's answer since it is an awful hack that breaks consistency with the rest of the apps on the platform.

you must use

android:showAsAction="never"

and let the android device decide if the overflow menu is needed for that device.



回答3:

This could be another work around, which really helped me. Keep one drawable with three dots and give it as a menu item.

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


<item
    android:id="@+id/saveDetails"
    android:showAsAction="always"
    android:title="@string/save"/>

<item
    android:id="@+id/menu_overflow"
    android:icon="@drawable/dts"
    android:orderInCategory="11111"
    android:showAsAction="always">
    <menu>
        <item
            android:id="@+id/contacts"
            android:showAsAction="never"
            android:title="Contacts"/>


        <item
            android:id="@+id/service_Tasks"
            android:showAsAction="never"
            android:title="Service Tasks"/>


        <item
            android:id="@+id/charge_summary"
            android:showAsAction="never"
            android:title="Charge Summary"/>


        <item
            android:id="@+id/capture_signature"
            android:showAsAction="never"
            android:title="Capture Signature"/>
    </menu>
</item>

 </menu>


回答4:

If I understand your question correctly and you want to show all your icons on the action bar change this parameters in your menu items

android:showAsAction="ifRoom|withText"

to this

android:showAsAction="always"


回答5:

If you are using from supporting libraries, in order to show menu items in action bar you must use from the following syntax in your xml:

yourappname:showAsAction="ifRoom|withText"


回答6:

The phones which have menu hardware button show extra menu items on click of the hardware button. Newer phones, with no hardware menu button automatically add an Overflow menu icon to the Action Bar. The extra menu items are those which have "showAsAction" property set to never.



回答7:

Within AndroidManifest.xml between add

android:theme="@android:style/Theme.Holo.Light"

which adds the action bar in your application.

After that, go to menu.xml and add the followings

xmlns:tools="http://schemas.android.com/tools

between Finally in each item add

android:showAsAction="always"
tools:ignore="AppCompatResource"