如何在不隐藏状态栏的情况下隐藏导航栏

huangapple go评论84阅读模式
英文:

How can I hide Navigation Bar without hiding the status bar

问题

我正在尝试隐藏底部导航栏,而不是状态栏。大多数代码都会同时隐藏导航栏和状态栏。

英文:

I am trying to hide the Bottom navigation bar only and not the status bar. Most of the code I am getting is hiding both the navigation bar and the status bar.

答案1

得分: 1

尝试下面的函数。由于Android R的更改,View.SYSTEM_UI_FLAG_HIDE_NAVIGATIONsetSystemUiVisibility等已被弃用。我已经制作了处理此情况的函数。

public void hideBottomNavigationBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        getWindow().setDecorFitsSystemWindows(false);
        WindowInsetsController controller = getWindow().getInsetsController();
        if (controller != null) {
            controller.hide(WindowInsets.Type.navigationBars());
            controller.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
        }
    } else {
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    }
}

关于使用View.SYSTEM_UI_FLAG_HIDE_NAVIGATION,还有一点需要注意,来自文档

存在一个限制:由于导航控件非常重要,最小的用户交互将导致它们立即重新出现。发生这种情况时,这个标志和SYSTEM_UI_FLAG_FULLSCREEN都将自动清除,以便同时重新出现这两个元素。

为了解决这个限制,您还可以像这样设置View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

但这仅适用于SDK 19及更高版本。

英文:

Try the below function. Owing to changes in Android R, View.SYSTEM_UI_FLAG_HIDE_NAVIGATION, setSystemUiVisibility etc are being deprecated. I have made the function to handle this scenario.

public void hideBottomNavigationBar() {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
         getWindow().setDecorFitsSystemWindows(false);
         WindowInsetsController controller = getWindow().getInsetsController();
         if(controller != null) {
           controller.hide(WindowInsets.Type.navigationBars());
           controller.setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
         }
      } else {
         getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

      }
}

Another thing to note about using View.SYSTEM_UI_FLAG_HIDE_NAVIGATION from the docs:

> There is a limitation: because navigation controls are so important, the least user interaction will cause them to reappear immediately. When this happens, both this flag and SYSTEM_UI_FLAG_FULLSCREEN will be cleared automatically, so that both elements reappear at the same time.

Inorder to work around this limitation you can set View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY as well like this:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);

But this only works SDK 19 onwards.

答案2

得分: 0

View v = getWindow().getDecorView();  
int options = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION  
            | View.SYSTEM_UI_FLAG_FULLSCREEN;  
v.setSystemUiVisibility(options); 
英文:
View v = getWindow().getDecorView();  
int options = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION  
            | View.SYSTEM_UI_FLAG_FULLSCREEN;  
v.setSystemUiVisibility(options); 

huangapple
  • 本文由 发表于 2020年7月21日 23:06:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63017524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定