简体   繁体   中英

Android, get screen width and height?

I am trying to get the width and height of the screen in Android, i have:

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

The width is 1280 but its coming back with 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? 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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