首页 > 解决方案 > 从底部滑动出现导航栏时如何隐藏BottomNavigationView?

问题描述

当从屏幕底部边缘滑动并出现系统导航栏时,我想隐藏 BottomNavigationView。现在系统导航栏是半透明的,并且悬停在 BottomNavigationView 上方。

活动 XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="start"
    android:fitsSystemWindows="false">
    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <include
            layout="@layout/include_toolbar"/>
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/appbar"/>
        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="56dp"
            android:layout_alignParentBottom="true"
            android:background="@color/app_background"
            app:itemIconTint="@color/main_text_color"
            app:labelVisibilityMode="unlabeled"
            android:paddingHorizontal="@dimen/normal_margin"
            app:elevation="2dp"
            app:menu="@menu/bottom_navigation_main" 
            android:layout_gravity="bottom"/>
    </android.support.design.widget.CoordinatorLayout>
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

片段 OnResume():

public override void OnResume()
        {
            base.OnResume();

            if (ParentActivity.BottomNavigation != null)
                ParentActivity.BottomNavigation.Visibility = ViewStates.Visible;

            View decorview = ParentActivity.Window.DecorView;
            var uiOptions =
                SystemUiFlags.HideNavigation |
                SystemUiFlags.ImmersiveSticky;
            decorview.SystemUiVisibility = (StatusBarVisibility)uiOptions;
        }

片段实现 View.IOnSystemUiVisibilityChangeListener

public void OnSystemUiVisibilityChange([GeneratedEnum] StatusBarVisibility visibility)
        {
            if (((int)visibility & (int)SystemUiFlags.Fullscreen) == 0)
            {
                if (ParentActivity.BottomNavigation != null)
                    ParentActivity.BottomNavigation.Visibility = ViewStates.Gone;
            }
            else
            {
                if (ParentActivity.BottomNavigation != null)
                    ParentActivity.BottomNavigation.Visibility = ViewStates.Visible;
            }
        }

侦听器不会对导航栏的外观做出反应。
滑动后导航栏出现时不调用 OnSystemUiVisibilityChange()

标签: c#androidxamarin.android

解决方案


你需要OnSystemUiVisibilityChangeListener- https://developer.android.com/reference/android/view/View.OnSystemUiVisibilityChangeListener

例如,在您的活动中onCreate()

window.decorView.setOnSystemUiVisibilityChangeListener { visibility ->
    // Note that system bars will only be "visible" if none of the
    // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
    if (visibility and 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.
    }
}

然后,您可以使底部导航视图的可见性与系统 UI 保持同步。


推荐阅读