如何获得设备的屏幕尺寸?

yos*_*i24 33 android

我想得到一个Android屏幕的高度,如果屏幕在一定高度,我将如何去做这个?

evi*_*one 59

如果您想要显示尺寸(以像素为单位),可以使用以下代码:

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth();
int height = display.getHeight();
Run Code Online (Sandbox Code Playgroud)

然后,您可以添加比较高度的条件以满足您的需求.

以英寸来算:

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);
Run Code Online (Sandbox Code Playgroud)

  • 这是错误的!,这不是px的计算,而是计算倾角.http://stackoverflow.com/questions/6840904/how-to-get-the-screen-size-of-the-device/6841335#6841335 (2认同)

ina*_*ruk 20

从活动内部:

int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight();
Run Code Online (Sandbox Code Playgroud)

或者如果你只有Context对象:

WindowManager windowManager = (WindowManager)mContext.getSystemService(WINDOW_SERVICE);
int width = windowManager.getDefaultDisplay().getWidth();
int height = windowManager.getDefaultDisplay().getHeight()
Run Code Online (Sandbox Code Playgroud)

更新.如何检测您的应用程序在大屏幕上运行:

//Android Level 9 and up:
Configuration config = getResources().getConfiguration();
if((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) ==  
    Configuration.SCREENLAYOUT_SIZE_XLARGE) 
{
    //xlarge screen
}
Run Code Online (Sandbox Code Playgroud)