这是我们获取屏幕高度的方法:
getResources().getDisplayMetrics().heightPixels;
我们遇到的问题是,由于Galaxy S8的隐藏导航栏,返回的值是高度减去导航栏大小(即使用户隐藏了导航栏)。我们如何获取完整可用的高度?例如:屏幕高度为2220像素,但返回的值是2076。
答案需要能够在有或没有隐藏导航栏的Galaxy S8以及其他设备上使用。非常感谢您的帮助。
这是我们获取屏幕高度的方法:
getResources().getDisplayMetrics().heightPixels;
尝试使用以下内容:
Point displaySize = new Point();
activity.getWindowManager().getDefaultDisplay().getRealSize(displaySize);
Point是来自android.graphics包的一个类。//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));
}
});
检查导航栏何时隐藏。
我曾经遇到过同样的问题,最终通过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 S8