这就是我们获取屏幕高度的方法:
getResources().getDisplayMetrics().heightPixels;我们遇到的问题是,对于Galaxy S8的隐藏导航栏,返回的值是高度减去导航栏大小(即使用户隐藏了导航栏)。我们如何获得完整的可用高度?
示例:屏幕高度为2220像素,但返回值为2076
答案需要能够在有或没有隐藏导航栏的Galaxy S8以及其他设备上工作
提前谢谢你
发布于 2017-12-07 04:30:02
尝试使用以下命令:
Point displaySize = new Point();
activity.getWindowManager().getDefaultDisplay().getRealSize(displaySize);Point是android.graphics包中的一个类。
在不减去任何窗口装饰或应用任何兼容性比例因子的情况下获得显示器的实际大小。
大小根据显示器的当前旋转进行调整。
当窗口管理器正在模拟较小的显示器时,实际尺寸可以小于屏幕的物理尺寸(使用adb外壳wm尺寸)。
编辑:
我在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));
}
});检查导航栏何时隐藏。
发布于 2018-06-19 17:24:33
我也有同样的问题,可以用decorview解决这个问题:
//Get the correct screen size even if the device has a hideable navigation bar (e.g. the Samsung Galaxy S8)
View decorView = getWindow().getDecorView(); //if you use this in a fragment, use getActivity before getWindow()
Rect r = new Rect();
decorView.getWindowVisibleDisplayFrame(r);
int screenHeight = r.bottom; // =2220 on S8 with hidden NavBar and =2076 with enabled NavBar
int screenWidth = r.right; // =1080 on S8https://stackoverflow.com/questions/47682826
复制相似问题