我需要检查设备是否有软导航栏,我遵循了here的建议。
它工作得很好,除了在onePlus设备上,出于某种原因,这段代码:
int id = resources.getIdentifier("config_showNavigationBar", "bool", android");
return id > 0 && resources.getBoolean(id);返回false,但会显示软导航栏。
你知道怎样才能得到正确的结果吗?
我不喜欢计算实际宽度和可用宽度,这似乎是昂贵的操作。
谢谢。
发布于 2016-10-19 17:50:40
请参阅this answer。然而,没有办法百分之百地确定。
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
boolean hasHomeKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_HOME);
if (hasBackKey && hasHomeKey) {
// no navigation bar, unless it is enabled in the settings
} else {
// 99% sure there's a navigation bar
}编辑
另一种方法
public boolean hasNavBar (Resources resources) {
int id = resources.getIdentifier("config_showNavigationBar", "bool", "android");
return id > 0 && resources.getBoolean(id);
}发布于 2016-10-19 19:31:48
是的,你可以试试这个:
WindowManager mgr = (WindowManager) getSystemService(WINDOW_SERVICE);
boolean hasSoftKey = Utils.hasSoftKeys(mgr, NPTApplication.this);
public static boolean hasSoftKeys(WindowManager windowManager, Context c) {
boolean hasSoftwareKeys = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
Display d = windowManager.getDefaultDisplay();
DisplayMetrics realDisplayMetrics = new DisplayMetrics();
d.getRealMetrics(realDisplayMetrics);
int realHeight = realDisplayMetrics.heightPixels;
int realWidth = realDisplayMetrics.widthPixels;
DisplayMetrics displayMetrics = new DisplayMetrics();
d.getMetrics(displayMetrics);
int displayHeight = displayMetrics.heightPixels;
int displayWidth = displayMetrics.widthPixels;
hasSoftwareKeys = (realWidth - displayWidth) > 0 || (realHeight - displayHeight) > 0;
} else {
boolean hasMenuKey = ViewConfiguration.get(c).hasPermanentMenuKey();
boolean hasBackKey = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK);
hasSoftwareKeys = !hasMenuKey && !hasBackKey;
}
return hasSoftwareKeys;
}发布于 2016-10-19 04:48:49
https://stackoverflow.com/questions/37699161
复制相似问题