Navigation Bar全屏适配
最近经常碰到屏幕适配的问题,尤其全面屏越来越多,国产手机各种奇葩造型的出现给我们适配带来了很大的困扰。今天说一下,因为底部导航栏“Navigation Bar”出现的问题。
因为底部导航栏的原因,挡住了最有一条信息。
/**
* 判断虚拟导航栏是否显示
*
* @param context 上下文对象
* @param window 当前窗口
* @return true(显示虚拟导航栏),false(不显示或不支持虚拟导航栏)
*/
public boolean checkNavigationBarShow(@NonNull Context context, @NonNull Window window) {
boolean show;
Display display = window.getWindowManager().getDefaultDisplay();
Point point = new Point();
display.getRealSize(point);
View decorView = window.getDecorView();
Configuration conf = context.getResources().getConfiguration();
if (Configuration.ORIENTATION_LANDSCAPE == conf.orientation) {
View contentView = decorView.findViewById(android.R.id.content);
show = (point.x != contentView.getWidth());
} else {
Rect rect = new Rect();
decorView.getWindowVisibleDisplayFrame(rect);
show = (rect.bottom != point.y);
}
return show;
}
/**
* 获取NavigationBar的大小,(长,宽)
* @param context Context
* @return (长,宽)
*/
public Point getNavigationBarSize(Context context) {
Point appUsableSize = getAppUsableScreenSize(context);
Point realScreenSize = getRealScreenSize(context);
if (null != appUsableSize && null != realScreenSize) {
// navigation bar on the side
if (appUsableSize.x < realScreenSize.x) {
return new Point(realScreenSize.x - appUsableSize.x, appUsableSize.y);
}
// navigation bar at the bottom
if (appUsableSize.y < realScreenSize.y) {
return new Point(appUsableSize.x, realScreenSize.y - appUsableSize.y);
}
// navigation bar is not present
return new Point();
} else {
return null;
}
}
private Point getAppUsableScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (null != windowManager) {
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
display.getSize(size);
return size;
} else {
return null;
}
}
private Point getRealScreenSize(Context context) {
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
if (null != windowManager) {
Display display = windowManager.getDefaultDisplay();
Point size = new Point();
if (Build.VERSION.SDK_INT >= 17) {
display.getRealSize(size);
} else if (Build.VERSION.SDK_INT >= 14) {
try {
size.x = (Integer) Display.class.getMethod("getRawWidth").invoke(display);
size.y = (Integer) Display.class.getMethod("getRawHeight").invoke(display);
} catch (Exception e) {
return null;
}
}
return size;
} else {
return null;
}
}
如何调用:
Point point =
NavigationBarUtils.getInstance().getNavigationBarSize(bv);
if (null != point) {
if (NavigationBarUtils.getInstance().checkNavigationBarShow(mActivity , mActivity.getWindow())) {
menuWindow.showAtLocation(v, Gravity.BOTTOM, 0, LPUtils.dip2px(44) + point.y);
} else {
menuWindow.showAtLocation(v, Gravity.BOTTOM, 0, LPUtils.dip2px(44));
}
} else {
menuWindow.showAtLocation(v, Gravity.BOTTOM, 0, LPUtils.dip2px(44));
}
point的(x,y)分别表示底部Navigation Bar的长和宽,如果point不为空的情况下说明Navigation Bar存在。调用checkNavigationBarShow获取导航栏是否展示。这样就能完美的解决这样的问题。