网上很多说各种适配的,在布局顶部的添加属性android:fitsSystemWindows="true"就好了,可是我自己测试过,很多手机都不行,只能手动对业务处理了,什么情况显示,什么情况隐藏,状态栏了
/** * 隐藏虚拟按键,并且全屏 */ public static void hideBottomUIMenu(Context context) { //隐藏虚拟按键,并且全屏 if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api View v = ((Activity) context).getWindow().getDecorView(); v.setSystemUiVisibility(View.GONE); } else if (Build.VERSION.SDK_INT >= 19) { //for new api versions. View decorView = ((Activity) context).getWindow().getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE; decorView.setSystemUiVisibility(uiOptions); } }
/**
* 显示菜单栏
* 如果底部的bar 隐藏就显示
*/
protected void showBottomUIMenu() {
int flags;
int curApiVersion = android.os.Build.VERSION.SDK_INT;
// This work only for android 4.4+
if (curApiVersion >= Build.VERSION_CODES.KITKAT) {
// This work only for android 4.4+
// hide navigation bar permanently in android activity
// touch the screen, the navigation bar will not show
flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
} else {
// touch the screen, the navigation bar will show
flags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
}
// must be executed in main thread :)
getWindow().getDecorView().setSystemUiVisibility(flags);
}
到这里结束了?然而,如果存在沉浸式的布局,显示隐藏状态栏的时候,会顶上去,导致顶部的状态栏显示不全
所以这里给出,获取底部菜单栏高度的方法,有需要的同学可以拿去:
private static final String NAVIGATION = "navigationBarBackground"; // 该方法需要在View完全被绘制出来之后调用,否则判断不了 //在比如 onWindowFocusChanged()方法中可以得到正确的结果 /*** * 该方法需要在View完全被绘制出来之后调用,否则判断不了 在比如 onWindowFocusChanged()方法中可以得到正确的结果 * @param activity * @return */ public static boolean isNavigationBarExist(@NonNull Activity activity) { ViewGroup vp = (ViewGroup) activity.getWindow().getDecorView(); if (vp != null) { for (int i = 0; i < vp.getChildCount(); i++) { vp.getChildAt(i).getContext().getPackageName(); if (vp.getChildAt(i).getId() != NO_ID && NAVIGATION.equals(activity.getResources().getResourceEntryName(vp.getChildAt(i).getId()))) { return true; } } } return false; } /*** * 获取底部菜单栏高度 * @param mActivity * @return */ public static int getNavigationBarHeight(Activity mActivity) { Resources resources = mActivity.getResources(); int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android"); int height = resources.getDimensionPixelSize(resourceId); Log.e("Navi height:" + height); return height; }
拿去不谢哦,希望可以帮到大家