2011-07-05 40 views

回答

56

如果你想在像素的顯示尺寸您可以使用此代碼:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth(); 
int height = display.getHeight(); 

然後你就可以添加條件比較高,以滿足您的需要。

在英寸:

DisplayMetrics dm = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(dm); 
double x = Math.pow(dm.widthPixels/dm.xdpi,2); 
double y = Math.pow(dm.heightPixels/dm.ydpi,2); 
double screenInches = Math.sqrt(x+y); 
Log.d("debug","Screen inches : " + screenInches); 
+0

那麼,如果我想按英寸走一步呢? – yoshi24

+1

@ yoshi24,編輯我的答案... – evilone

+0

作品像一個魅力,謝謝! – yoshi24

19

從活動中:

int width = getWindowManager().getDefaultDisplay().getWidth(); 
int height = getWindowManager().getDefaultDisplay().getHeight(); 

或者,如果你只有Context對象:

WindowManager windowManager = (WindowManager)mContext.getSystemService(WINDOW_SERVICE); 
int width = windowManager.getDefaultDisplay().getWidth(); 
int height = windowManager.getDefaultDisplay().getHeight() 

修訂。如何檢測大屏幕上的應用程序運行:

//Android Level 9 and up: 
Configuration config = getResources().getConfiguration(); 
if((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == 
    Configuration.SCREENLAYOUT_SIZE_XLARGE) 
{ 
    //xlarge screen 
} 
+0

好,謝謝!那麼我會如何比較呢? 10.2英寸的屏幕?像什麼指標呢? – yoshi24

+0

更新了答案。 – inazaruk

+0

酷!謝謝你,那實際上更好! – yoshi24

0

在你的onCreate或任何其他活動的方法簡單地做:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth(); 
int height = display.getHeight(); 
相關問題