//获取是否存在NavigationBar public static boolean checkDeviceHasNavigationBar(Context context) { //通过判断设备是否有返回键、菜单键(不是虚拟键,是手机屏幕外的按键)来确定是否有navigation bar boolean hasMenuKey = ViewConfiguration.get(context) .hasPermanentMenuKey(); boolean hasBackKey = KeyCharacterMap .deviceHasKey(KeyEvent.KEYCODE_BACK); if (!hasMenuKey && !hasBackKey) { // 做任何你需要做的,这个设备有一个导航栏 return true; } return false; } //获取是否存在NavigationBar public static boolean checkHuaWeiDeviceHasNavigationBar(Context context) { boolean hasNavigationBar = false; try { Resources rs = context.getResources(); int id = rs.getIdentifier("config_showNavigationBar", "bool", "android"); if (id > 0) { hasNavigationBar = rs.getBoolean(id); } Class systemPropertiesClass = Class.forName("android.os.SystemProperties"); Method m = systemPropertiesClass.getMethod("get", String.class); String navBarOverride = (String) m.invoke(systemPropertiesClass, "qemu.hw.mainkeys"); if ("1".equals(navBarOverride)) { hasNavigationBar = false; } else if ("0".equals(navBarOverride)) { hasNavigationBar = true; } } catch (Exception e) { } return hasNavigationBar; } //NavigationBar状态是否是显示 public boolean isNavigationBarShow() { Activity mContext = (Activity) context; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { Display display = mContext.getWindowManager().getDefaultDisplay(); Point size = new Point(); Point realSize = new Point(); display.getSize(size); display.getRealSize(realSize); return realSize.y != size.y; } else { boolean menu = ViewConfiguration.get(context).hasPermanentMenuKey(); boolean back = KeyCharacterMap.deviceHasKey(KeyEvent.KEYCODE_BACK); if (menu || back) { return false; } else { return true; } }}
//获取NavigationBar高度
private int getNavigationBarHeight() { Resources resources = context.getResources(); int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); int height = resources.getDimensionPixelSize(resourceId); Log.v("dbw", "Navi height:" + height); return height; }之后重写onMeasure方法在该方法中进行判断是否存在NavigationBar,若存在判断NavigationBar的显示与隐藏,之后重新计算高度
关于华为手机自定义view中NavigationBar显示与隐藏造成高度问题的解决办法
最新推荐文章于 2022-11-04 07:50:30 发布