如何始终显示溢出菜单(动作条),即使手机有硬件菜单按钮? [英] How to always show overflow menu (ActionBar) even if phone has hardware menu button?

查看:200
本文介绍了如何始终显示溢出菜单(动作条),即使手机有硬件菜单按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下提到的codeS实现。请建议我怎么即使手机有硬件菜单按钮创建操作栏上的溢出菜单中的选项?

Following mentioned are codes implemented. Please suggest me how to create the "overflow menu" option in action bar even if a phone have the hardware menu button?

这是我目前的code:

This is my current code:

MainActivity.java

MainActivity.java

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_main_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

activity_main_actions.xml

activity_main_actions.xml

<?xml version="1.0" encoding ="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

<!-- Search  -->
<item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      android:showAsAction="ifRoom"/>

<!-- Email -->
<item android:id="@+id/action_email"
      android:icon="@drawable/ic_action_email"
      android:title="@string/action_email"
      android:showAsAction="never"/>

<!-- Help -->
<item android:id="@+id/action_help"
      android:icon="@drawable/ic_action_help"
      android:title="@string/action_help"
      android:showAsAction="never"/>

<!-- attach -->
<item android:id="@+id/action_attach"
      android:icon="@drawable/ic_action_attachment"
      android:title="@string/action_attach"
      android:showAsAction="never" />

推荐答案

我终于得到了答案毕竟有以下code,这是方法onCreateOptionMenu()之后,codeD ...

Finally i got the answer after all with the following code, it was coded after the method onCreateOptionMenu()...

import android.view.ViewConfiguration;

        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();
        }
    }

这篇关于如何始终显示溢出菜单(动作条),即使手机有硬件菜单按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆