Android 计算屏幕高度错误

1,838 阅读1分钟

在Android的传统的计算屏幕宽高方法是

public static int getScreenWidth() {
    return Resources.getSystem().getDisplayMetrics().widthPixels;
}

public static int getScreenHeight() {
    return Resources.getSystem().getDisplayMetrics().heightPixels;
}

但是这个在高版本的机型上,因为有高航栏的存在,导致得到的高度是屏幕的高度减去导航栏的高度

换一种方式

public static int getScreenHeightSize(Context context) {
        WindowManager wm = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            wm.getDefaultDisplay().getRealSize(point);
        } else {
            wm.getDefaultDisplay().getSize(point);
        }
        return point.y;//获取宽度
    }

这样获取的就是完整的高度了