1. 获取顶部statusBar高度
private int getStatusBarHeight() { Resources resources = mActivity.getResources(); int resourceId = resources.getIdentifier("status_bar_height", "dimen","android"); int height = resources.getDimensionPixelSize(resourceId); Log.v("dbw", "Status height:" + height); return height; }2. 获取底部navigationBar高度
private int getNavigationBarHeight() { Resources resources = mActivity.getResources(); int resourceId = resources.getIdentifier("navigation_bar_height","dimen", "android"); int height = resources.getDimensionPixelSize(resourceId); Log.v("dbw", "Navi height:" + height); return height; }3. 获取设备是否存在NavigationBar
//获取是否存在NavigationBar public static boolean checkDeviceHasNavigationBar(Context context) { boolean hasNavigationBar = false; Resources rs = context.getResources(); int id = rs.getIdentifier("config_showNavigationBar", "bool", "android"); if (id > 0) { hasNavigationBar = rs.getBoolean(id); } try { 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; }需要注意的是:
1)没显示导航栏的,方法二还是会返回导航栏的值,而不是返回0,所以要结合方法三使用
2)如果是沉浸式透明的导航栏,如全面屏手机,三星S8,方法三返回true,即存在导航栏,所以如果有些布局是以距离底部多高为标注,就要注意了!透明导航栏实际上是不影响布局的。