java - 硬件菜单单击从底部弹出,而不是从工具栏弹出

标签 java android

<item
        android:id="@+id/members"
        android:orderInCategory="100"
        android:title="@string/members"
        app:showAsAction="never" />


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





 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button

            case R.id.members:
                Intent intent = new Intent(this, GroupDetailActivity.class);
                intent.putExtra("name", toolbarText);
                intent.putExtra("groupId", currentGroupId);
                startActivity(intent);
                break;

            case android.R.id.home:
                finish();

                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

我有一个 Activity ,工具栏包含一个主页和一个项目成员,代码工作正常,但如果我点击设备上的硬件菜单按钮,菜单项成员将显示在设备底部,而不是工具栏的 anchor

最佳答案

将此代码 fragment 添加到您的 onCreate 方法中,

ActionBar actionBar = getActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
         try {
                ViewConfiguration config = ViewConfiguration.get(this);
                Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
                if(menuKeyField != null) {
                    menuKeyField.setAccessible(true);
                    menuKeyField.setBoolean(config, false);
                }
            } catch (Exception ex) {
                // Ignore
            }

关于java - 硬件菜单单击从底部弹出,而不是从工具栏弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32880071/

相关文章:

java - 将 URL 中的值获取到 JSP 中

java - 关于 JCS 的信息

android - 包 android.support.v4.view 不存在,即使在为它添加依赖项之后

android - 在 Google-TV Sony 3.2 Android 程序中运行 HLS 流时如何停止(隐身)URL 抑制

java - 通过java代码初始化EditTextPreference的defaultValue

java - AggregationResults 类型转换 MongoDB + Spring 数据 + 聚合

java - 在ANDROID中将内部文件内容写入LISTVIEW

android - 配置文件/调试 APK

android - 在 fragment android中查看重用

java - 将 SNAPSHOT 部署到 oss.jfrog.org (JCenter)