gpt4 book ai didi

java - 获取实际屏幕高度(android)

转载 作者:搜寻专家 更新时间:2023-10-30 21:36:40 26 4
gpt4 key购买 nike

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

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 是获取屏幕高度的糟糕方法吗?

最佳答案

参见 this回答

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

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

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

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

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

更新

对于 API 17 及更高版本,方法 Display.getRealSize() 返回屏幕的全尺寸,如前所述 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.

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).

关于java - 获取实际屏幕高度(android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35780980/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com