在Android4.0以上设备的虚拟按键中显示menu键

在 Android4.0以后,google添加了虚拟导航键来替换实体键,到现在Android7.0了基本上导航键上已经没有菜单键了,再查看下网上各种资料之后,研究了下源码,发现是否显示菜单键实在 Window初始化的布局中判断的,也即PhoneWindow的generateLayout函数

在Android 5.1 以下该函数部分代码是这样实现的:

final Context context = getContext();  

final int targetSdk = context.getApplicationInfo().targetSdkVersion;  

final boolean targetPreHoneycomb = targetSdk < android.os.Build.VERSION_CODES.HONEYCOMB;  

final boolean targetPreIcs = targetSdk < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;  

final boolean targetHcNeedsOptions = context.getResources().getBoolean(  

        com.android.internal.R.bool.target_honeycomb_needs_options_menu);  

final boolean noActionBar = !hasFeature(FEATURE_ACTION_BAR) || hasFeature(FEATURE_NO_TITLE);  


if (targetPreHoneycomb || (targetPreIcs && targetHcNeedsOptions && noActionBar)) {  

    addFlags(WindowManager.LayoutParams.FLAG_NEEDS_MENU_KEY);  

}else {  

    clearFlags(WindowManager.LayoutParams.FLAG_NEEDS_MENU_KEY);  



但是再看Android 6.0 的源码,PhoneWindow的generateLayout函数却稍微改变了点:

final Context context = getContext();  

final int targetSdk = context.getApplicationInfo().targetSdkVersion;  

final boolean targetPreHoneycomb = targetSdk < android.os.Build.VERSION_CODES.HONEYCOMB;  

final boolean targetPreIcs = targetSdk < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH;  

final boolean targetPreL = targetSdk < android.os.Build.VERSION_CODES.LOLLIPOP;  

final boolean targetHcNeedsOptions = context.getResources().getBoolean(  

        R.bool.target_honeycomb_needs_options_menu);  

final boolean noActionBar = !hasFeature(FEATURE_ACTION_BAR) || hasFeature(FEATURE_NO_TITLE);  


if (targetPreHoneycomb || (targetPreIcs && targetHcNeedsOptions && noActionBar)) {  

    setNeedsMenuKey(WindowManager.LayoutParams.NEEDS_MENU_SET_TRUE);  

}else {  

    setNeedsMenuKey(WindowManager.LayoutParams.NEEDS_MENU_SET_FALSE);  

}  

而Android5.1.1的generateLayout函数的那部分代码跟6.0基本上是一样的,所以暂时只看Android 6.0的代码。

在Android6.0的Window类中多了一个方法,setNeedMenuKey 函数,该函数的作用就是设置是否显示虚拟菜单键,在Android5.1.1之前是否显示菜单键是WindowManager.LayoutParams 中的一个flags,而在Android5.1.1及以后,google把这个标记为改到了WindowManager.LayoutParams类中的needsMenuKey 字段去了,可以通过setNeedMenuKey

方法来修改。

说了这么多,也大概明白了怎么回事了,下面就给出一个通用的解决方法,利用反射来完成:


private void setNeedsMenuKey() {  

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {  

return;  

  }  

if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {  

try {  

int flags = WindowManager.LayoutParams.class.getField("FLAG_NEEDS_MENU_KEY").getInt(null);  

          getWindow().addFlags(flags);  

}catch (IllegalAccessException e) {  

          e.printStackTrace();  

}catch (NoSuchFieldException e) {  

          e.printStackTrace();  

      }  

}else {  

try {  

Method setNeedsMenuKey = Window.class.getDeclaredMethod("setNeedsMenuKey", int.class);  

setNeedsMenuKey.setAccessible(true);  

int value = WindowManager.LayoutParams.class.getField("NEEDS_MENU_SET_TRUE").getInt(null);  

          setNeedsMenuKey.invoke(getWindow(), value);  

}catch (NoSuchMethodException e) {  

          e.printStackTrace();  

}catch (NoSuchFieldException e) {  

          e.printStackTrace();  

}catch (IllegalAccessException e) {  

          e.printStackTrace();  

}catch (InvocationTargetException e) {  

          e.printStackTrace();  

      }  

  }  

该函数就可以强行设置在虚拟导航栏中显示菜单按钮,调用位置的话,我是放在setContentView函数之后使用的,亲测有效:

setContentView(R.layout.activity_main);  

setNeedsMenuKey();  

以上就是所有的内容,如有不对的地方,欢迎各位Android开发的小伙伴们一起研究讨论。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在日常开发中,我们经常会遇到viewpager配合fragment的情况;这些fragment有的要求顶进状态栏,...
    我想做个程序猿阅读 6,235评论 0 1
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,544评论 25 708
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,569评论 2 45
  • 说什么我爱你,你爱我 还不都是 毕了业,分了手 再找个人,过上不愠不火的生活
    四月16阅读 184评论 0 1
  • 第一次有偿做教练,发现了很多问题。 自己总是不愿意去想明白很多东西,只想去做一个很好的执行者,比如练习爬行,教练教...
    112233D阅读 288评论 3 0