当应用程序在兼容模式下运行时,如何获得实际的屏幕大小

时间:2011-10-12 09:21:56

标签: android screen

我尝试使用DeviceMetrics读取屏幕大小,但它给出了缩放的大小,而不是实际的屏幕大小。

如何在兼容模式下阅读实际屏幕尺寸?

例如,应用程序在480x800屏幕上运行 但由于兼容模式,它给了我400x700。

1 个答案:

答案 0 :(得分:0)

试试这段代码:

    if (Build.VERSION.SDK_INT >= 11) {
        Point point= new Point();
        getWindowManager().getDefaultDisplay().getRealSize(point);
        screenWidth = point.x;
        screenHeight = point.y;
    } else {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        screenWidth = displayMetrics.widthPixels;
        screenHeight = displayMetrics.heightPixels;
    }

注意:由于方法getRealSize,您可能需要捕获NoSuchMethodError异常