在 Android 4.0以后,google添加了虚拟导航键来替换实体键,同时按键由原来的四大天王改为back、home、recent三个。研究源码可以发现是否显示菜单键是在 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);
- }
而Android 5.1.1的generateLayout函数的那部分代码跟6.0基本上是一样的,所以暂时只看Android 6.0的代码。
在Android 6.0的Window类中多了一个方法,setNeedMenuKey 函数,该函数的作用就是设置是否显示虚拟菜单键,在Android 5.1.1之前是否显示菜单键是WindowManager.LayoutParams 中的一个flags,而在Android 5.1.1及以后,google把这个标记为改到了WindowManager.LayoutParams类中的needsMenuKey 字段去了,可以通过setNeedMenuKey 方法来修改。
说了这么多,也大概明白了怎么回事了,下面就给出一个通用的解决方法,利用反射来完成:
- private void showNagivationMenuKey() {
- 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);
- showNavigationMenuKey();
在Activity中重写onKeyDown方法来监听Menu键:
- public boolean onKeyDown(int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_MENU) {
-
- return true;
- }
- return super.onKeyDown(keyCode, event);
- }