Java 获取实际屏幕高度(android)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35780980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 17:07:07  来源:igfitidea点击:

Getting the actual screen height (android)

javaandroidandroid-studio

提问by Taegos

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. 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.heightPixelsa bad way of getting the screen height?

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

采纳答案by Rosario Vivas

see thisanswer

看到这个答案

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)时,实际大小可能小于屏幕的物理大小。

回答by Makvin

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

回答by lenooh

I needed to get the actualavailable 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 activityRootViewis the root view of your layout.

activityRootView布局的根视图在哪里。

If you need to extract the toolbar height:

如果需要提取工具栏高度:

int availableScreenHeight = screenHeight - toolbar.getHeight();

where toolbaris your toolbar view.

toolbar您的工具栏视图在哪里。

availableScreenHeightwill now be without the statusbar, without the toolbar, and without the navigation bar (if the device is using it).

availableScreenHeight现在将没有状态栏、工具栏和导航栏(如果设备正在使用它)。

回答by Meysam Keshvari

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