Android获取顶部状态栏statusBar高度、底部导航栏navigationBar高度

手机顶部显示时间、电量等信息的叫状态栏,即statusBar
有些手机比如华为在底部会有返回、回到主页面等虚拟功能键,这是导航栏,即navigationBar

下面列出一些项目中常用的功能代码? 嘻嘻……

  • 1.获取顶部statusBar高度
private int getStatusBarHeight() {
    Resources resources = mActivity.getResources();
    int resourceId = resources.getIdentifier("status_bar_height", "dimen","android");
    int height = resources.getDimensionPixelSize(resourceId);
    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);
    return height;
}
  • 3.获取设备是否存在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) {
            //do something
        }
        return hasNavigationBar;
}
  • 4.获取屏幕高度
public static int getScreenHeight(Context context) {
     DisplayMetrics dm = context.getResources().getDisplayMetrics();
     return dm.heightPixels;
}
  • 5.获取屏幕宽度
public static int getScreenWidth(Context context) {
     DisplayMetrics dm = context.getResources().getDisplayMetrics();
     return dm.widthPixels;
 }

 



作者:stone呀
链接:https://www.jianshu.com/p/cbba19018f5d
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

你可能感兴趣的:(Android开发)