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

I use these codes to get the height and width of screen

the screen height on my Nexus7 should be 1280

but it return 1205...

and my minSdkVersion is level 8

so i can't use these method:

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

now, how should i get the correct screen size ?

Edit:

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;
    }

use it works!

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 に答える