android-如何获取屏幕的宽度和高度
我试图使用以下代码在android应用程序开发中获取屏幕宽度和高度:
Display display = getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
但是我的display收到了NullPointerException,为什么? 如何获得屏幕的宽度和高度呢?
14个解决方案
127 votes
如果要在Activity之外调用此方法,则需要传入上下文(或通过其他调用获得上下文)。 然后使用它来获取显示指标:
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
更新:使用API级别17+,您可以使用getRealMetrics:
Point displaySize = new Point();
activity.getWindowManager().getDefaultDisplay().getRealSize(displaySize);
如果需要可用的窗口大小,可以使用getRealMetrics通过从实际显示大小中减去装饰视图大小来计算可用区域:
Point displaySize = new Point();
activity.getWindowManager().getDefaultDisplay().getRealSize(displaySize);
Rect windowSize = new Rect();
ctivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(windowSize);
int width = displaySize.x - Math.abs(windowSize.width());
int height = displaySize.y - Math.abs(windowSize.height());
return new Point(width, height);
getRealMetrics也可以工作(需要API级别17+),但是我还没有尝试过:
DisplayMetrics metrics = new DisplayMetrics();
activity.GetWindowManager().getDefaultDisplay().getRealMetrics(metrics);
areyling answered 2020-01-04T18:45:06Z
35 votes
在活动中,您可以致电:
int width = this.getResources().getDisplayMetrics().widthPixels;
int height = this.getResources().getDisplayMetrics().heightPixels;
在视图中时,首先需要获取上下文:
int width = getApplicationContext().getResources().getDisplayMetrics().widthPixels;
int height = getApplicationContext().getResources().getDisplayMetrics().heightPixels;
这将适用于所有Android版本(自API 1开始可用),并且永不弃用。
Khulja Sim Sim answered 2020-01-04T18:45:35Z
18 votes
从服务:
Display display= ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
Maidul answered 2020-01-04T18:45:55Z
15 votes
这终于对我有用:
DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
fritz answered 2020-01-04T18:46:15Z
11 votes
如果您有可用的上下文,请尝试通过context.getResources().getDisplayMetrics()。
MrJre answered 2020-01-04T18:46:35Z
7 votes
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
我想您编写的代码已过时。
Housefly answered 2020-01-04T18:46:54Z
4 votes
if (Build.VERSION.SDK_INT >= 11) {
Point size = new Point();
try {
this.getWindowManager().getDefaultDisplay().getRealSize(size);
screenWidth = size.x;
screenHeight = size.y;
} catch (NoSuchMethodError e) {
screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
screenWidth=this.getWindowManager().getDefaultDisplay().getWidth();
}
} else {
DisplayMetrics metrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
}
Sujith Manjavana answered 2020-01-04T18:47:10Z
2 votes
尝试使用以下代码来获取屏幕的宽度和高度
int widthOfscreen =0;
int heightOfScreen = 0;
DisplayMetrics dm = new DisplayMetrics();
try {
((Activity) context).getWindowManager().getDefaultDisplay()
.getMetrics(dm);
} catch (Exception ex) {
}
widthOfscreen = dm.widthPixels;
heightOfScreen = dm.heightPixels;
Sunil Kumar Sahoo answered 2020-01-04T18:47:30Z
2 votes
WindowManager w = getWindowManager();
Display d = w.getDefaultDisplay();
DisplayMetrics metrics = new DisplayMetrics();
d.getMetrics(metrics);
Log.d("WIDTH: ", String.valueOf(d.getWidth()));
Log.d("HEIGHT: ", String.valueOf(d.getHeight()));
DallinDyer answered 2020-01-04T18:47:45Z
2 votes
/*
*DisplayMetrics: A structure describing general information about a display, such as its size, density, and font scaling.
* */
DisplayMetrics metrics = getResources().getDisplayMetrics();
int DeviceTotalWidth = metrics.widthPixels;
int DeviceTotalHeight = metrics.heightPixels;
IntelliJ Amiya answered 2020-01-04T18:48:01Z
1 votes
int scrWidth = getWindowManager().getDefaultDisplay().getWidth();
int scrHeight = getWindowManager().getDefaultDisplay().getHeight();
Axxel answered 2020-01-04T18:48:16Z
0 votes
Display display = getActivity().getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
Log.d("Tag", "Getting Width >> " + screenWidth);
Log.d("Tag", "Getting Height >> " + screenHeight);
这在我的应用程序中正常工作
Rudolf Coutinho answered 2020-01-04T18:48:36Z
0 votes
使用功能的替代方式
private int[] getScreenSIze(){
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int h = displaymetrics.heightPixels;
int w = displaymetrics.widthPixels;
int[] size={w,h};
return size;
}
在按钮上单击或在您的创建函数上添加以下代码
int[] screenSize= getScreenSIze();
int width=screenSize[0];
int height=screenSize[1];
screenSizes.setText("Phone Screen sizes \n\n width = "+width+" \n Height = "+height);
查看演示源代码
Daniel Nyamasyo answered 2020-01-04T18:49:05Z
0 votes
低于和低于10的答案支持api级别
if (Build.VERSION.SDK_INT >= 11) {
Point size = new Point();
try {
this.getWindowManager().getDefaultDisplay().getRealSize(size);
screenWidth = size.x;
screenHeight = size.y;
} catch (NoSuchMethodError e) {
screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
screenWidth=this.getWindowManager().getDefaultDisplay().getWidth();
}
} else {
DisplayMetrics metrics = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
screenWidth = metrics.widthPixels;
screenHeight = metrics.heightPixels;
}
Rahul answered 2020-01-04T18:49:25Z