美文网首页
Android 获取NavigationBar高度

Android 获取NavigationBar高度

作者: JokAr_ | 来源:发表于2017-05-15 21:42 被阅读424次

    使用方法如下:

    Point point = getNavigationBarSize(this);
    bottomHeight = point.y;
     if (bottomHeight > 0) {
         //有NavigationBar
     }
    

    public static Point getNavigationBarSize(Context context) {
            Point appUsableSize = getAppUsableScreenSize(context);
            Point realScreenSize = getRealScreenSize(context);
    
            // navigation bar on the right
            if (appUsableSize.x < realScreenSize.x) {
                return new Point(realScreenSize.x - appUsableSize.x, appUsableSize.y);
            }
    
            // navigation bar at the bottom
            if (appUsableSize.y < realScreenSize.y) {
                return new Point(appUsableSize.x, realScreenSize.y - appUsableSize.y);
            }
    
            // navigation bar is not present
            return new Point();
        }
    
        @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
        public static Point getAppUsableScreenSize(Context context) {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = windowManager.getDefaultDisplay();
            Point size = new Point();
            display.getSize(size);
            return size;
        }
    
        public static Point getRealScreenSize(Context context) {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = windowManager.getDefaultDisplay();
            Point size = new Point();
    
            if (Build.VERSION.SDK_INT >= 17) {
                display.getRealSize(size);
            } else if (Build.VERSION.SDK_INT >= 14) {
                try {
                    size.x = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
                    size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
                } catch (IllegalAccessException e) {
                } catch (InvocationTargetException e) {
                } catch (NoSuchMethodException e) {
                }
            }
    
            return size;
        }
    

    相关文章

      网友评论

          本文标题:Android 获取NavigationBar高度

          本文链接:https://www.haomeiwen.com/subject/pwckxxtx.html