您的位置:首页 > 移动开发 > Android开发

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

2016-10-13 23:52 603 查看
在 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开发的小伙伴们一起研究讨论。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息