Android Overflow Menu Button is not visible on some phones

て烟熏妆下的殇ゞ 提交于 2019-12-12 06:35:29

问题


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



来源:https://stackoverflow.com/questions/30159810/android-overflow-menu-button-is-not-visible-on-some-phones

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!