2016-06-15 6 views
2

私の電話はHUAWEI NXT-AL10で、システム設定ページから解像度が1080 x 1920であることがわかりますが、画面解像度を取得するために以下のようなコードを書くと、値は1080 x 1794、おそらくソフトキーボードのサイズは含まれていないと思います。では、設定ページ(1080 x 1920)と同じように、どのように解像度を得ることができますか?ソフトキーボードのサイズを含む画面の解像度を取得する方法は?

DisplayMetrics metric = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(metric); 
    int width = metric.widthPixels; 
    int height = metric.heightPixels; 

答えて

1

以下のコードは、あなたがこのAPI 17と@SimonH答えを

+0

API 17でうまくいきました。 API 16で実際のサイズを取得する方法? – sunjinbo

+0

私は週末を離れてあなたのコメントを見ました。私は自分のアプリケーションのメトリクスを提供するために使用していたコードをコピーしたばかりです。私はあなたの完全な反応が好きです。しかしAPI13で 'getHeight'と' getWidth'は廃止されましたか? – SimonH

1

おかげ上を必要とし、私はより多くのロジックを追加することを

Point size = new Point(); 
activity.getWindowManager().getDefaultDisplay().getRealSize(size); 
iWidth = size.x; 
iHeight = size.y; 

Log.i(TAG, "Screen real size (pixels) :width = " + iWidth); 
Log.i(TAG, "Screen real size (pixels) :height = " + iHeight); 

注意探しているものをあなたを与える必要がありますAPI 16以下をサポートしています。ここでは参考にしています:

private void initScreenSize() { 
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { 
     Point size = new Point(); 
     getWindowManager().getDefaultDisplay().getRealSize(size); 
     mScreenWidth = size.x; 
     mScreenHeight = size.y; 
    } else { 
     Display display = getWindowManager().getDefaultDisplay(); 

     try { 
      Method getHeight = Display.class.getMethod("getRawHeight"); 
      Method getWidth = Display.class.getMethod("getRawWidth"); 
      mScreenWidth = (Integer) getHeight.invoke(display); 
      mScreenHeight = (Integer) getWidth.invoke(display); 
     } catch (Exception e) { 
      mScreenWidth = display.getWidth(); 
      mScreenHeight = display.getHeight(); 
     } 
    } 
} 
関連する問題