android - 盖乐世 S8 : screen height returning wrong value

标签 android

这就是我们获取屏幕高度的方式:

getResources().getDisplayMetrics().heightPixels;

我们遇到的问题是,对于 Galaxy S8 的隐藏导航栏,返回的值是高度减去导航栏大小(即使导航栏被用户隐藏)。我们如何获得完整的可用高度?

示例:屏幕高度为 2220 像素但返回值为 2076

答案需要能够在隐藏或不隐藏导航栏的 Galaxy S8 以及其他设备上工作

提前谢谢你

最佳答案

尝试使用这个:

Point displaySize = new Point();
activity.getWindowManager().getDefaultDisplay().getRealSize(displaySize);

Pointandroid.graphics 包中的一个类。

来自文档:https://developer.android.com/reference/android/view/Display.html#getRealSize(android.graphics.Point)

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

编辑:

我在 LG G4 上检查了这段代码,它工作正常:

//get the full height of device
      Point displaySize = new Point();
      getWindowManager().getDefaultDisplay().getRealSize(displaySize);

      Resources resources = getResources();
            int navBarId = resources.getIdentifier("navigation_bar_height", "dimen", "android");


          int fullHeight = displaySize.y;
//get nav bar size
          int navbarSize = resources.getDimensionPixelSize(navBarId);
          int availableSize = fullHeight;
//check is navbar is visible
          boolean navBarVisible = (getWindow().getDecorView().getSystemUiVisibility() &
                        View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0;
          if(navBarVisible){
                    availableSize -= navbarSize;
                }

您也可以使用此代码:

view.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int i) {
                Log.d("Nav bar visible : ", String.valueOf((i & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)==0));
            }
        });

检查导航栏何时隐藏。

关于android - 盖乐世 S8 : screen height returning wrong value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47682826/

相关文章:

android - 在哪里可以找到用户安装的证书 android 4.0 及更高版本

Android Ndk 在 Windows 上安装?

c# - 如何在Xamarin中使用MediaPlayer获得跟踪的持续时间

android - 401 Unauthorized error.将授权码升级为凭证对象失败

android 如何在 ScrollView 中添加 ListView

android - 在 Android 项目中使用 StateFlow

java - ExpandableListView:从子项获取输入值

android - 错误 : Cannot find setter for field. - java.util.ArrayList 中的大小 - Room 中的嵌入式 ArrayList 无法编译

android - 重新创建类似于 Android 12 设置的开关

android - 将 AutoCompleteTextView 与中等大小的字符串数据集一起使用的最佳方法