最近在全局搜索里开发一个浏览器,碰到了底部点击新建窗口,闪屏,有时候点击事件还没反应按下去会显示主题,最后发现是对导航栏的处理问题,底部导航栏处理前需要判断是否可见,可见再隐藏,不可见就不用。
/**
* 隐藏虚拟按键,并且全屏
*/
public static void hideBottomUIMenu(AppCompatActivity context,Window window) {
//隐藏虚拟按键,并且全屏
if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
View v = context.getWindow().getDecorView();
v.setSystemUiVisibility(View.GONE);
} else if (Build.VERSION.SDK_INT >= 19) {
//for new api versions.
View decorView = context.getWindow().getDecorView();
final int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
if (isNavBarVisible(window)){
decorView.setSystemUiVisibility(uiOptions);
}
}
}
public static boolean isNavBarVisible(@NonNull final Window window) {
boolean isVisible = false;
ViewGroup decorView = (ViewGroup) window.getDecorView();
for (int i = 0, count = decorView.getChildCount(); i < count; i++) {
final View child = decorView.getChildAt(i);
final int id = child.getId();
if (id != View.NO_ID) {
String resourceEntryName = getResNameById(id);
if ("navigationBarBackground".equals(resourceEntryName)
&& child.getVisibility() == View.VISIBLE) {
isVisible = true;
break;
}
}
}
if (isVisible) {
// 对于三星手机,android10以下非OneUI2的版本,比如 s8,note8 等设备上,
// 导航栏显示存在bug:"当用户隐藏导航栏时显示输入法的时候导航栏会跟随显示",会导致隐藏输入法之后判断错误
// 这个问题在 OneUI 2 & android 10 版本已修复
int visibility = decorView.getSystemUiVisibility();
isVisible = (visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0;
}
return isVisible;
}
private static String getResNameById(int id) {
try {
return ZSApplication.getAppContext().getResources().getResourceEntryName(id);
} catch (Exception ignore) {
return "";
}
}