因为手机硬件情况的不同,在没有物理Menu键的手机上。ActionBar的overflowbutton会有显示不出来的情况,能够通过反射的方式改动ViewConfiguration类中的sHasPermanentMenuKey静态变量的值永远为false。系统就是依据这个变量值来推断手机有没有物理Menu键,代码例如以下:
@Override
protected void onCreate(Bundle savedInstanceState) {
……
setOverflowShowingAlways();
}
private void setOverflowShowingAlways() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField(“sHasPermanentMenuKey”);
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
……
setOverflowShowingAlways();
}
private void setOverflowShowingAlways() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class.getDeclaredField(“sHasPermanentMenuKey”);
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
} catch (Exception e) {
e.printStackTrace();
}
}