在Android中立即隐藏导航栏

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

Hide navigation bar without delay in Android

问题

我有一个简单的应用程序,它在后台加载一个ImageView

<ImageView
    android:id="@+id/splashimage"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="centerCrop"
    android:adjustViewBounds="true"
    android:src="@drawable/logo"
    android:visibility="gone"/>

我试图以全屏模式加载图像,即没有导航栏和应用栏。我已经能够通过设置以下标志来在运行应用程序的第一刻删除appbar

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

然而,当我尝试对应用程序底部的导航栏执行相同操作时,会有一些延迟才能将其移除。因此,行为有点奇怪。发生的情况是首先显示导航栏,然后隐藏它并显示白色背景,只有在一秒钟后图像才填充这个空白。我附上一张图片以尽量清晰地再现这种情况。

我的问题是,如何在一开始就显示图像覆盖那个空间,而不是按照下面提到的三个步骤的行为?

要隐藏导航栏,可以使用以下代码:

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

在Android中立即隐藏导航栏

英文:

I have a simple application which loads in the background an ImageView.

&lt;ImageView
    android:id=&quot;@+id/splashimage&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:scaleType=&quot;centerCrop&quot;
    android:adjustViewBounds=&quot;true&quot;
    android:src=&quot;@drawable/logo&quot;
    android:visibility=&quot;gone&quot;/&gt;

I am trying to load the image in full screen mode, meaning no navigation bar and no app bar. I have been able to remove te appbar from the very first moment that I run the application by setting the following flag:

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

Nevertheless, when I try to do the same with the navigation bar at the bottom of the application, there is some delay before getting removed. Therefore, the behaviour is a bit weird. What it happens is that first the navigation bar is shown, next it is hidden and shows a white background, and only after a second the image fills the gap. I attach a photo to try to reproduce the situation as clear as possible.

My question is, how could I show the image covering that space from the beginning rather than following the 3 steps behaviour mentioned below?

To hide the navigation bar this code is used;

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

在Android中立即隐藏导航栏

答案1

得分: 1

经过跟随Bruno的评论,我已经更新了我的代码并解决了错误:

getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE
            // 将内容设置为出现在系统栏下方,以便在系统栏隐藏和显示时内容不会重新调整大小。
            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            // 隐藏导航栏和状态栏
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

请注意,我并不想完全移除appbar。因此,我没有在末尾包含以下行 | View.SYSTEM_UI_FLAG_FULLSCREEN);

更新

请注意,如果您希望您的布局出现在appbar下方而不是其下方,则需要移除此行:

| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
英文:

After following Bruno s comment I have updated my code and solve the error:

 getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE
            // Set the content to appear under the system bars so that the
            // content doesn&#39;t resize when the system bars hide and show.
            | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            // Hide the nav bar and status bar
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

Note that I did not want to remove completely the appbar. Therefore I did not include the following line | View.SYSTEM_UI_FLAG_FULLSCREEN); at the end.

Update

Note that if you want your layout to appear under the appbar and not under it, this line needs to be removed

                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE

答案2

得分: 0

这段代码在我导航到的碎片中始终有效,我将此代码放在MainActivity中:

        navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);

        navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
            @Override
            public void onDestinationChanged(@NonNull NavController navController,
                                             @NonNull NavDestination navDestination,
                                             @Nullable Bundle bundle) {
                BottomNavigationView navBar = findViewById(R.id.nav_view);
                if(navDestination.getId() == R.id.navigation_librarySlideShowFragment ||
                        navDestination.getId() == R.id.navigation_settings ||
                        navDestination.getId() == R.id.wifiSettingsFragment ||
                        navDestination.getId() == R.id.playbackFragment) {
                    navBar.setVisibility(View.GONE);
                } else {
                    navBar.setVisibility(View.VISIBLE);
                }
            }
英文:

This worked for me without fail for fragments to which I navigated, I put this code in MainActivity

        navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);

        navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
            @Override
            public void onDestinationChanged(@NonNull NavController navController,
                                             @NonNull NavDestination navDestination,
                                             @Nullable Bundle bundle) {
                BottomNavigationView navBar = findViewById(R.id.nav_view);
                if(navDestination.getId() == R.id.navigation_librarySlideShowFragment ||
                        navDestination.getId() == R.id.navigation_settings ||
                        navDestination.getId() == R.id.wifiSettingsFragment ||
                        navDestination.getId() == R.id.playbackFragment) {
                    navBar.setVisibility(View.GONE);
                } else {
                    navBar.setVisibility(View.VISIBLE);
                }
            }

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

发表评论

匿名网友

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

确定