英文:
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);
英文:
I have a simple application which loads in the background an 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"/>
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);
答案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'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);
                }
            }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论