This question already has an answer here:
这个问题在这里已有答案:
- Get screen dimensions in pixels 36 answers
获取屏幕尺寸以像素为单位36个答案
I would like to get the height of a android screen and if the screen inst a certain height, how would i go about doing this?
我想得到一个Android屏幕的高度,如果屏幕不是一定的高度,我将如何做到这一点?
3 个解决方案
#1
58
If you want the display dimensions in pixels you can use this code:
如果您想要显示尺寸(以像素为单位),可以使用以下代码:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Then you can add condition that compares the height to satisfy your needs.
然后,您可以添加比较高度的条件以满足您的需求。
In inches:
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);
#2
19
From within activity:
从活动内部:
int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight();
Or if you only have Context
object:
或者如果您只有Context对象:
WindowManager windowManager = (WindowManager)mContext.getSystemService(WINDOW_SERVICE);
int width = windowManager.getDefaultDisplay().getWidth();
int height = windowManager.getDefaultDisplay().getHeight()
UPDATED. How to detect your application runs on large screen:
更新。如何检测您的应用程序在大屏幕上运行:
//Android Level 9 and up:
Configuration config = getResources().getConfiguration();
if((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) ==
Configuration.SCREENLAYOUT_SIZE_XLARGE)
{
//xlarge screen
}
#3
0
In your onCreate or any other Activity method simply do:
在你的onCreate或任何其他Activity方法中,只需:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
#1
58
If you want the display dimensions in pixels you can use this code:
如果您想要显示尺寸(以像素为单位),可以使用以下代码:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Then you can add condition that compares the height to satisfy your needs.
然后,您可以添加比较高度的条件以满足您的需求。
In inches:
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);
#2
19
From within activity:
从活动内部:
int width = getWindowManager().getDefaultDisplay().getWidth();
int height = getWindowManager().getDefaultDisplay().getHeight();
Or if you only have Context
object:
或者如果您只有Context对象:
WindowManager windowManager = (WindowManager)mContext.getSystemService(WINDOW_SERVICE);
int width = windowManager.getDefaultDisplay().getWidth();
int height = windowManager.getDefaultDisplay().getHeight()
UPDATED. How to detect your application runs on large screen:
更新。如何检测您的应用程序在大屏幕上运行:
//Android Level 9 and up:
Configuration config = getResources().getConfiguration();
if((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) ==
Configuration.SCREENLAYOUT_SIZE_XLARGE)
{
//xlarge screen
}
#3
0
In your onCreate or any other Activity method simply do:
在你的onCreate或任何其他Activity方法中,只需:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();