安卓沉浸式状态栏适配有底部导航虚拟按键
如图 未适配和适配的两种效果图
代码实现
1.首先在onCreate()中
//适配底部导航虚拟栏if (StatusBarUtil.hasNavigationBarShow(this)) {getWindow().getDecorView().findViewById(android.R.id.content).setPadding(0, 0, 0, StatusBarUtil.getNavigationBarHeight(this));}
2.自己新建StatusBarUtil类中写
public static boolean hasNavigationBarShow(Activity activity) {if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {return false;}WindowManager wm = (WindowManager) activity.getSystemService(Context.WINDOW_SERVICE);Display display = wm.getDefaultDisplay();DisplayMetrics outMetrics = new DisplayMetrics();//获取整个屏幕的高度display.getRealMetrics(outMetrics);int heightPixels = outMetrics.heightPixels;int widthPixels = outMetrics.widthPixels;//获取内容展示部分的高度outMetrics = new DisplayMetrics();display.getMetrics(outMetrics);int heightPixelsContent = outMetrics.heightPixels;int widthPixelsContent = outMetrics.widthPixels;int h = heightPixels - heightPixelsContent;int w = widthPixels - widthPixelsContent;return w > 0 || h > 0; //竖屏和横屏两种情况}/*** 获取导航栏高度** @param context* @return*/public static int getNavigationBarHeight(Context context) {return getSystemComponentDimen(context, "navigation_bar_height");}public static int getSystemComponentDimen(Context context, String dimenName) {// 反射手机运行的类:android.R.dimen.status_bar_height.int statusHeight = -1;try {Class<?> clazz = Class.forName("com.android.internal.R$dimen");Object object = clazz.newInstance();String heightStr = clazz.getField(dimenName).get(object).toString();int height = Integer.parseInt(heightStr);//dp->pxstatusHeight = context.getResources().getDimensionPixelSize(height);} catch (Exception e) {e.printStackTrace();}return statusHeight;}
搞定