在Android软按键中添加Menu键

时间:2023-03-09 21:25:07
在Android软按键中添加Menu键

在Android中,有时候没有硬件Menu。于是需要软件Menu。但是默认Google想让你用ActionBar上的Menu。

但是,有时候我们确实需要在在底部的软按键上加Menu。这里提供一种方法,经过测试,可行。

这里,设置Window的属性,要求添加Menu键。

为什么用反射,是因为这个是隐藏的属性。并且,在低版本上,这个属性是不存在的。

另外,软按键的显示,是在SystemUI里面控制的,这里没有详细分析,有时间可以分析一下。

try {

getWindow().addFlags(WindowManager.LayoutParams.class.getField("FLAG_NEEDS_MENU_KEY").getInt(null));

}

catch (NoSuchFieldException e) {

// Ignore since this field won't exist in most versions of Android

LogMi.w(TAG, "cant find field FLAG_NEEDS_MENU_KEY at WindowManager.LayoutParams");

}

catch (IllegalAccessException e) {

Log.w(TAG, "Could not access FLAG_NEEDS_MENU_KEY in addLegacyOverflowButton()", e);

}