简体   繁体   English

Android,获取屏幕的宽度和高度吗?

[英]Android, get screen width and height?

I am trying to get the width and height of the screen in Android, i have: 我正在尝试获取Android屏幕的宽度和高度,我有:

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
stageWidth = displaymetrics.widthPixels;
stageHeight = displaymetrics.heightPixels;

The width is 1280 but its coming back with 752. 宽度为1280,但宽度为752。

Any ideas? 有任何想法吗?

Try this instead: 尝试以下方法:

Display display = getWindowManager().getDefaultDisplay(); 
stageWidth = display.getWidth();
stageHeight = display.getHeigth();

Is that the same width? 那是一样的宽度吗? Are you sure the width is 1280 and not 752? 您确定宽度是1280而不是752吗? What device are you testing it on and what height are you getting? 您要在什么设备上进行测试,并且要达到什么高度?

Rotate your screen and see.I think you will get your correct screen size. 旋转屏幕看看。我认为您会得到正确的屏幕尺寸。

The above code worked with me and should work with you. 上面的代码与我一起工作,应该与您一起工作。

在展示对象上使用此方法(来自docs):

void getSize(Point outSize) - Returns the raw size of the display, in pixels.

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

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