【问题标题】:Android actionbar menu icon now shown in tabletAndroid 操作栏菜单图标现在显示在平板电脑中
【发布时间】:2014-11-02 16:59:01
【问题描述】:

我开发了一个在我的操作栏中包含一个菜单图标的应用程序,我创建的菜单如下:

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

这是 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);
    }
}

使用我的智能手机菜单图标在那里(使用 LG 手机),但是当我在我的平板电脑(Galaxy Tab 8)上测试它时,菜单图标消失了,但功能仍然存在。按下底部的菜单软按钮,弹出窗口,但顶部操作栏中缺少图标。如何解决?有什么想法吗?

【问题讨论】:

    标签: android android-actionbar android-menu


    【解决方案1】:

    在带有硬件菜单按钮的设备(例如三星 Galaxy 系列)上,溢出菜单通过使用硬件菜单按钮表现为“传统”菜单。

    请为每个菜单项使用以下属性

    android:showAsAction="always"
    

    如果您使用的是 actionbarcompat pack ,则使用以下属性代替上述属性

    app:showAsAction="always"
    

    不要忘记添加以下命名空间

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

    【讨论】:

    • 感谢山姆的回复,经过更多搜索后,我发现正如您在带有菜单按钮硬件的设备上提到的那样,它不起作用,尽管将操作设置为 showAsAction="always"将显示项目本身而不是菜单图标,但我在此链接中找到了解决方案:stackoverflow.com/questions/9739498/…
    【解决方案2】:

    正如我发现的那样,在提供菜单按钮硬件的设备上,菜单图标将被隐藏,并且使菜单项 showAsAction="always" 不会将菜单图标带回我打算提出的问题中,所以我'找到了解决方案Here

    这是我从上面的链接中使用的代码来解决这个问题(如果它可以被称为问题):

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

    你可以把这个函数放在你的活动或基础活动中,然后在你的 onCreate 函数中调用它,它会为你做所有的魔法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多