46
DisplayMetrics metrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;

我使用这些代码来获取屏幕的高度和宽度

我的 Nexus7 的屏幕高度应该是 1280

但它返回 1205 ......

我的 minSdkVersion 是 8 级

所以我不能使用这些方法:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screen_width = size.x;
int screen_height = size.y;

现在,我应该如何获得正确的屏幕尺寸?

编辑:

if (Build.VERSION.SDK_INT >= 11) {
        Point size = new Point();
        try {
            this.getWindowManager().getDefaultDisplay().getRealSize(size);
            screenWidth = size.x;
            screenHeight = size.y;
        } catch (NoSuchMethodError e) {
            Log.i("error", "it can't work");
        }

    } else {
        DisplayMetrics metrics = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        screenWidth = metrics.widthPixels;
        screenHeight = metrics.heightPixels;
    }

使用它有效!

4

3 回答 3

50

我编写了一个方法,该方法将尽可能接近 API 10 到 17+。它应该为 API 14+ 提供真实的宽度和高度,并且它对其他所有东西都做得很好。

    Display display = context.getWindowManager().getDefaultDisplay();
    int realWidth;
    int realHeight;

    if (Build.VERSION.SDK_INT >= 17){
        //new pleasant way to get real metrics
        DisplayMetrics realMetrics = new DisplayMetrics();
        display.getRealMetrics(realMetrics);
        realWidth = realMetrics.widthPixels;
        realHeight = realMetrics.heightPixels;

    } else if (Build.VERSION.SDK_INT >= 14) {
        //reflection for this weird in-between time
        try {
            Method mGetRawH = Display.class.getMethod("getRawHeight");
            Method mGetRawW = Display.class.getMethod("getRawWidth");
            realWidth = (Integer) mGetRawW.invoke(display);
            realHeight = (Integer) mGetRawH.invoke(display);
        } catch (Exception e) {
            //this may not be 100% accurate, but it's all we've got
            realWidth = display.getWidth();
            realHeight = display.getHeight();
            Log.e("Display Info", "Couldn't use reflection to get the real display metrics.");
        }

    } else {
        //This should be close, as lower API devices should not have window navigation bars
        realWidth = display.getWidth();
        realHeight = display.getHeight();
    }
于 2014-05-26T00:46:39.010 回答
39

我认为这会奏效。诀窍是你必须使用:

display.getRealSize(size);

不是

display.getSize(size);

要处理您的 API 8 编码问题,请执行以下操作:

try { 
    display.getRealSize(size);
    height = size.y; 
} catch (NoSuchMethodError e) {
    height = display.getHeight();
}

只有更新的 API 设备才会有屏幕导航按钮,因此需要新方法,旧设备会抛出异常但不会有屏幕导航,因此旧方法很好。

如果需要说明:仅仅因为您的项目的最低 API 级别为 8 并不意味着您必须在该级别编译它。我也至少使用 8 级,但我的项目大部分是在 13 级(3.2)编译的,让他们可以访问很多新方法。

于 2013-01-15T16:34:07.853 回答
4

您可以使用 usingDisplayDisplayMetrics

使用显示:

private void findRealSize(Activity activity)
{
    Point size = new Point();
    Display display = activity.getWindowManager().getDefaultDisplay();

    if (Build.VERSION.SDK_INT >= 17)
        display.getRealSize(size);
    else
        display.getSize(size);

    int realWidth = size.x;
    int realHeight = size.y;

    Log.i("LOG_TAG", "realWidth: " + realWidth + " realHeight: " + realHeight);
}

使用 DisplayMetrics:

private void findRealSize(Activity activity)
{
    DisplayMetrics displayMetrics = new DisplayMetrics();

    if (Build.VERSION.SDK_INT >= 17)
    {
        activity.getWindowManager().getDefaultDisplay().getRealMetrics(displayMetrics);
    }
    else
    {
        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    }

    int realWidth = displayMetrics.widthPixels;
    int realHeight = displayMetrics.heightPixels;

    Log.i("LOG_TAG", "realWidth: " + realWidth + " realHeight: " + realHeight);
}
于 2017-11-07T10:18:03.657 回答