Android 如何获得你的安卓设备的屏幕分辨率..?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11338602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:02:59  来源:igfitidea点击:

How to get screen resolution of your android device..?

androidcordovascreen-resolution

提问by Rikin Thakkar

Can i get resolution of android phone..?
if yes then how..?
it will really helpful for me..
Thank you..

我可以得到安卓手机的分辨率吗..?
如果是,那么如何..?
这对我真的很有帮助..
谢谢..

回答by K_Anas

If you want the display dimensions in pixels you can use getSize:

如果您想要以像素为单位的显示尺寸,您可以使用 getSize:

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

This method was introduced in Android API 13, so if you want to get display metrics for previous APIs use:

此方法是在 Android API 13 中引入的,因此如果您想获取以前 API 的显示指标,请使用:

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;

If you're not in an Activity, you can get the default Display via WINDOW_SERVICE. Also be sure to set your minSdkVersion to at least 4 in AndroidManifest.xml to enable calls to display.getWidth().

如果您不在活动中,则可以通过WINDOW_SERVICE. 还要确保在 AndroidManifest.xml 中将 minSdkVersion 设置为至少 4,以启用对 display.getWidth() 的调用。

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

Edit: PhoneGap

编辑:PhoneGap

Use the following to figure out the display size in pixels

使用以下方法计算以像素为单位的显示尺寸

 function getDeviceDimention() {
        console.log("Device Dimention using PhoneGap");
        console.log("Width = " + window.innerWidth);
        console.log("Height = " + window.innerHeight);
    }

回答by vee

For Cordova / Phonegap, I've found that using

对于 Cordova / Phonegap,我发现使用

var width = screen.width
var height = screen.height

is more reliable than using

比使用更可靠

var width = window.innerHeight
var height = window.innerWidth

in order to obtain the resolution of the current display. The latter is unfortunately inaccurate if your mark-up either overflows or does not take up all of the space available on the screen.

以获得当前显示器的分辨率。不幸的是,如果您的标记溢出或没有占用屏幕上的所有可用空间,则后者是不准确的。

回答by Coder_sLaY

For phonegap you can use this

对于phonegap,您可以使用它

You can use device-widthand device-height

您可以使用设备宽度设备高度

<meta name="viewport" content="width=device-width; user-scalable=no" />

You can even get device height and width using jquery & jquery mobile like this

您甚至可以像这样使用 jquery & jquery mobile 获取设备高度和宽度

var width = $(window).width();
var height = $(window).height();

NOTE:-Do this when device is ready.

注意:-在设备准备就绪时执行此操作。

回答by user2008830

You can call

你可以打电话

Display display = getWindow().getWindowManager().getDefaultDisplay();
Point outSize = new Point();
display.getRealSize(outSize);

outSize.x is the width and outSize.y is the height. Hope it help.

outSize.x 是宽度,outSize.y 是高度。希望有帮助。

回答by RajaReddy PolamReddy

like this

像这样

Display display = getWindowManager().getDefaultDisplay(); 
int width = display.getWidth(); 
int height = display.getHeight();  

look at this answer Android: How to get screen dimensions

看看这个答案Android:如何获取屏幕尺寸

回答by Ilya Demidov

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;

Good luck!

祝你好运!

回答by Kernelzero

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

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

good luck ??? ??.

祝你好运 ?????.

回答by Aamirkhan

u can get screen resolution like this

你可以得到这样的屏幕分辨率

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