简体   繁体   English

获取实际屏幕高度(android)

[英]Getting the actual screen height (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);

The device that I run using the emulator has a screen width of 1080 pixels and a screen height of 1920 pixels.我使用模拟器运行的设备屏幕宽度为 1080 像素,屏幕高度为 1920 像素。 The width is displayed correctly (1080 pixels) but the height is according to the app only 1776 pixels.宽度显示正确(1080 像素),但高度根据应用只有 1776 像素。 What do I need to make my app display the correct screen height?我需要什么才能使我的应用程序显示正确的屏幕高度? Is metrics.heightPixels a bad way of getting the screen height? metrics.heightPixels是获取屏幕高度的糟糕方法吗?

see this answer看到这个答案

if your API level > 13 try this if you are in activity如果您的 API 级别 > 13,请在活动中尝试此操作

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

If you're not in an Activity you can get the default Display via WINDOW_SERVICE:如果您不在活动中,您可以通过 WINDOW_SERVICE 获得默认显示:

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

getWidth and getHeight is for API level 13 or less getWidth 和 getHeight 适用于 API 级别 13 或更低

UPDATE更新

For API 17 and higher method Display.getRealSize() returns full size of screen, as mentioned in documentation :对于 API 17 及更高版本的方法Display.getRealSize()返回屏幕的全尺寸,如文档中所述

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).当窗口管理器模拟较小的显示(使用 adb shell am display-size)时,实际大小可能小于屏幕的物理大小。

int width=Resources.getSystem().getDisplayMetrics().widthPixels;
int height=Resources.getSystem().getDisplayMetrics().heightPixels;

I needed to get the actual available height, and out of all the options this is the only thing that worked for me.我需要获得实际可用的高度,在所有选项中,这是唯一对我有用的方法。

Create a rectangle, and get its dimensions:创建一个矩形,并获取其尺寸:

Rect r = new Rect();
activityRootView.getWindowVisibleDisplayFrame(r);
int screenHeight = r.bottom - r.top;

where activityRootView is the root view of your layout.其中activityRootView是布局的根视图。

If you need to extract the toolbar height:如果需要提取工具栏高度:

int availableScreenHeight = screenHeight - toolbar.getHeight();

where toolbar is your toolbar view.其中toolbar是您的工具栏视图。

availableScreenHeight will now be without the statusbar, without the toolbar, and without the navigation bar (if the device is using it). availableScreenHeight现在将没有状态栏、工具栏和导航栏(如果设备正在使用它)。

you can use this code:您可以使用此代码:

val display = windowManager.defaultDisplay
val size = Point()
display.getSize(size)
val width: Int = size.x
val height: Int = size.y

You can try this:你可以试试这个:

 WindowManager wm = getWindowManager();
 Display d = wm.getDefaultDisplay();
 Point size = new Point();
 d.getSize(size);
 Log.i("LOG", "W=" + size.x + " , H=" + size.y);

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

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