这就是我们获取屏幕高度的方式:
getResources().getDisplayMetrics().heightPixels;
我们遇到的问题是,对于 Galaxy S8 的隐藏导航栏,返回的值是高度减去导航栏大小(即使导航栏被用户隐藏)。我们如何获得完整的可用高度?
示例:屏幕高度为 2220 像素但返回值为 2076
答案需要能够在隐藏或不隐藏导航栏的 Galaxy S8 以及其他设备上工作
提前谢谢你
最佳答案
尝试使用这个:
Point displaySize = new Point();
activity.getWindowManager().getDefaultDisplay().getRealSize(displaySize);
Point 是 android.graphics 包中的一个类。
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/