问题描述
我想获取运行我的应用程序的设备的实际屏幕高度.为此,我尝试以下操作:
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适用于13级或更低的API
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:
根据显示器的当前旋转角度来调整尺寸.
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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!