简体   繁体   English

平板电脑中现在显示了Android操作栏菜单图标

[英]Android actionbar menu icon now shown in tablet

I've developed an application which contains a menu icon in my actionbar, I create the menu as below: 我已经开发了一个应用程序,该应用程序的动作栏中包含一个菜单图标,如下所示创建菜单:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return true;
}

and here's the code for the onOptionsItemSelected: 这是onOptionsItemSelected的代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.menu_home:
        startActivity(new Intent(this, MainActivity.class));
        return true;
    case R.id.menu_adv_search:
        startActivity(new Intent(this, AdvSearchActivity.class));
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

using my smart phone the menu icon is there (using LG phone), but when I test it on my tablet (Galaxy Tab 8) the menu icon is gone, but the functionality is still there. 使用我的智能手机时,菜单图标在那里(使用LG手机),但是当我在平板电脑上测试它(Galaxy Tab 8)时,菜单图标消失了,但是功能仍然存在。 pressing the menu soft button at the bottom, the popup appears, but the icon is missing from the top action bar. 按下底部的菜单软按钮,将显示弹出窗口,但顶部操作栏中的图标缺失。 How to fix it? 如何解决? any ideas? 有任何想法吗?

On devices with hardware menu buttons ( for example Galaxy series of samsung) the overflow menu behaves as the 'traditional' menu, by using the hardware menu button. 在带有硬件菜单按钮的设备上(例如三星的Galaxy系列),通过使用硬件菜单按钮,溢出菜单的作用类似于“传统”菜单。

please use following attribute for each of your menu item 请为每个菜单项使用以下属性

android:showAsAction="always"

if you are using actionbarcompat pack , then instead of above attribute use following 如果您使用的是actionbarcompat pack,那么请使用以下命令代替上面的属性

app:showAsAction="always"

don't forget to add the following namespace aswell 别忘了还要添加以下名称空间

xmlns:app="http://schemas.android.com/apk/res-auto"

As I found out, on devices with menu button hardware provided, the menu icon will be hidden and making the menu item showAsAction="always" will not bring the menu icon back it in my question I was intending to, so I've found the solution Here 我发现,在提供了菜单按钮硬件的设备上,菜单图标将被隐藏,并且使菜单项showAsAction =“ always”不会在我打算的问题中再次显示菜单图标,因此我发现解决方案在这里

and here's the code I've used from the above link to overcome this issue(if it can be called an issue): 这是我从上面的链接中使用的代码来克服此问题(如果可以将其称为问题):

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

you can just put this function in your activity or base activity and call it in your onCreate function and it will do all the magic for ya. 您只需将此函数放在您的活动或基本活动中,然后在onCreate函数中调用它,它将为您带来所有的魔力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM