简体   繁体   English

如何获取屏幕的宽度和高度-Android

[英]How can I get the width and Height of the screen -Android

I tried using 我尝试使用

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

metrics.heightPixels;
metrics.widthPixels;

but because I use View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY the width is no right. 但是因为我使用View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY所以宽度不正确。 It shows the size without the navigation bar. 它显示没有导航栏的尺寸。 but I want to know all the screen size.. 但我想知道所有的屏幕尺寸。

Thanks! 谢谢!

This is only possible in API 17 and later. 这仅在API 17及更高版本中可行。 Just use getRealMetrics(metrics) instead of calling getMetrics() . 只需使用getRealMetrics(metrics)而不是调用getMetrics() (Or you can call getRealSize(Point) if all you need is the size.) For an approach to use with earlier API levels, see this thread . (或者,如果需要的只是大小,则可以调用getRealSize(Point) 。)有关与早期API级别一起使用的方法,请参见此线程

Its working fine for me 对我来说很好

DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;

Try 尝试

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

use this code this is implemented in API 13 使用此代码在API 13中实现

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

or 要么

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int width = display.getWidth();  // deprecated
int height = display.getHeight();  // deprecated

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM