获取实际屏幕高度(android) [英] Getting the actual screen height (android)

查看:41
本文介绍了获取实际屏幕高度(android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取运行我的应用程序的设备的实际屏幕高度.为此,我尝试以下操作:

I want to get the actual screen height of the device that runs my app. To achieve this i try the following:

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

int Height = metrics.heightPixels;
TextView HeightView = (TextView) findViewById(R.id.screenHeight);
HeightView.setText("Screen Height: " + Height);

int Width = metrics.widthPixels;
TextView WidthView = (TextView) findViewById(R.id.screenWidth);
WidthView.setText("Screen Width: " + Width);

我使用模拟器运行的设备的屏幕宽度为 1080 像素,屏幕高度为 1920 像素.宽度显示正确(1080 像素),但高度根据应用程序只有 1776 像素.我需要什么才能让我的应用显示正确的屏幕高度?metrics.heightPixels 是获取屏幕高度的糟糕方法吗?

The device that I run using the emulator has a screen width of 1080 pixels and a screen height of 1920 pixels. The width is displayed correctly (1080 pixels) but the height is according to the app only 1776 pixels. What do I need to make my app display the correct screen height? Is metrics.heightPixels a bad way of getting the screen height?

推荐答案

这个答案

如果您的 API 级别 > 13,请尝试此操作

if your API level > 13 try this if you are in activity

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

如果您不在活动中,您可以通过 WINDOW_SERVICE 获取默认显示:

If you're not in an Activity you can get the default Display via WINDOW_SERVICE:

WindowManager wm = (WindowManager)    context.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();

getWidth 和 getHeight 适用于 API 级别 13 或更低

getWidth and getHeight is for API level 13 or less

更新

对于 API 17 及更高版本的方法 Display.getRealSize() 返回屏幕的全尺寸,如前所述 在文档中:

For API 17 and higher method Display.getRealSize() returns full size of screen, as mentioned in documentation:

获取显示器的实际尺寸无需减去任何窗口装饰或应用任何兼容性比例因子.

Gets the real size of the display without subtracting any window decor or applying any compatibility scale factors.

根据显示器的当前旋转调整大小.

The size is adjusted based on the current rotation of the display.

实际尺寸可能小于屏幕实际尺寸时窗口管理器正在模拟一个较小的显示(使用 adb shell am显示尺寸).

The real size may be smaller than the physical size of the screen when the window manager is emulating a smaller display (using adb shell am display-size).

这篇关于获取实际屏幕高度(android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆