以下代码可以成功隐藏系统导航栏从屏幕上消失。用户仍然可以向上滑动以显示导航栏,导航栏会保持在屏幕上几秒钟,然后再次消失。
是否有回调函数来检测导航栏何时出现和消失,当用户向上滑动后,导航栏自动隐藏后?
下面的代码(摘自文档)似乎可以检测导航栏在启动时是否隐藏,但不能检测用户向上滑动以显示它或之后它消失的情况。
是否有回调函数来检测导航栏何时出现和消失,当用户向上滑动后,导航栏自动隐藏后?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
getWindow().setDecorFitsSystemWindows(false);
if (getWindow().getInsetsController() != null) {
getWindow().getInsetsController().hide(WindowInsets.Type.navigationBars());
getWindow().getInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
}
} else {
getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
);
}
下面的代码(摘自文档)似乎可以检测导航栏在启动时是否隐藏,但不能检测用户向上滑动以显示它或之后它消失的情况。
View decorView = getWindow().getDecorView();
decorView.setOnSystemUiVisibilityChangeListener
(new View.OnSystemUiVisibilityChangeListener() {
@Override
public void onSystemUiVisibilityChange(int visibility) {
// Note that system bars will only be "visible" if none of the
// LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
// TODO: The system bars are visible. Make any desired
// adjustments to your UI, such as showing the action bar or
// other navigational controls.
} else {
// TODO: The system bars are NOT visible. Make any desired
// adjustments to your UI, such as hiding the action bar or
// other navigational controls.
}
}
});