Android Overflow Menu Button is not visible on som

2019-09-08 03:43发布

问题:

The app runs fine. On devices with hard options menu button the menu shows up, so i know it works. On some devices it shows the overflow button at the top-right.

I my test case the device is Asus Zenphone 5 there is no hard button, i dont get a overflow button either. But when i run the showOptionsMenu() command from a button click it displays the options menu and all related events work no issues.

Menu - Xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/select" android:title="Select" android:showAsAction="never"></item>
    <item android:id="@+id/add_kid" android:title="Add" android:showAsAction="never"></item>
</menu>

onCreate & onPrepare

@Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {

        getMenuInflater().inflate(R.menu.menu1, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) 
    {
        if(!getCheckInStatus())
        {
            menu.setGroupVisible(R.id.group1,false);
        }
        else
        {
            menu.setGroupVisible(R.id.group1,true);
        }
        return super.onPrepareOptionsMenu(menu);
    }

Manifest values for the Activity

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
  .....

<activity
            android:name="com.my.package.MyActivity"
            android:configChanges="orientation|keyboard"
            android:label="@string/app_name"
            android:launchMode="singleInstance"

        >

        </activity>

I would really appreciate any help on this matter.

回答1:

I used to have same problem in few of my projects. I followed the steps given below which worked for me 1. extends your activity to ActionBarActivity

  1. call the following code in onCreate method

    private void makeActionOverflowMenuShown() 
    {
    //devices with hardware menu button (e.g. Samsung Note) don't show action overflow menu
    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) {
    }
       }
    


回答2:

set android:showAsAction="ifRoom" in menu.xml, the button would show.



回答3:

Try like this. This works for me..

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:icon="@drawable/ic_action_overflow"
        android:id="@+id/moreItmes"
        android:showAsAction="never"
        android:title="@string/more">        
        <menu>           
            <item
                android:id="@+id/select"
                android:showAsAction="never"
                android:title="@string/Select"/>

            <item
               android:id="@+id/add_kid"
                android:showAsAction="never"
                android:title="@string/Add"/>
        </menu>
    </item>

</menu>


回答4:

As i commented in vinay's answer , the reason was that i was debugging an old program written by some one else. It seems all were Activities , but i had changed the target and compile-with to 22. Once i changed the Activity to ActionBarActivity the overflow appeared on the devices with soft menu button. While those with hard options menu button did not display it.



回答5:

Menu - Xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@+id/select" android:title="Select" android:showAsAction="never"></item>
    <item android:id="@+id/add_kid" android:title="Add" android:showAsAction="never"></item>
</menu>

in your menu.xml you just use showAsAction as never which represent Never place this item in the Action Bar. please refer the link for more info