2011-07-05 41 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(); 
相关问题