You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android:阻止Bottom Navigation Bar滚动隐藏及单独隐藏Toolbar

嘿,这两个关于Bottom Navigation Bar的问题我之前都踩过坑,给你整理了亲测有效的解决办法:

问题1:阻止Bottom Navigation Bar随页面滚动隐藏

默认情况下,BottomNavigationView搭配CoordinatorLayout使用时,自带的BottomNavigationView.Behavior会让它在页面滚动时自动隐藏/显示。要干掉这个行为,有两种简单方式:

  • 方式一:直接移除布局里的Behavior属性
    找到你的BottomNavigationView标签,把app:layout_behavior="@string/bottom_navigation_view_behavior"这行删掉,或者改成app:layout_behavior=""。这样它就完全不响应滚动事件了,会一直固定在底部。

  • 方式二:自定义Behavior禁用滚动隐藏
    如果需要保留Behavior的其他功能(比如响应其他布局变化),只是不想让它随滚动隐藏,可以写个自定义Behavior:

    class NoScrollHideBottomNavBehavior : BottomNavigationView.Behavior() {
        override fun onStartNestedScroll(
            coordinatorLayout: CoordinatorLayout,
            child: BottomNavigationView,
            directTargetChild: View,
            target: View,
            axes: Int,
            type: Int
        ): Boolean {
            // 返回false,不监听滚动事件,自然就不会触发隐藏了
            return false
        }
    }
    

    然后在布局里引用这个自定义Behavior:

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:layout_behavior="com.yourpackage.NoScrollHideBottomNavBehavior"/>
    
问题2:仅隐藏Toolbar,不隐藏Bottom Navigation Bar

从你提到的app_bar_main.xml结构来看,应该是用了CoordinatorLayout包裹AppBarLayout(里面是Toolbar)和BottomNavigationView。出现隐藏Toolbar导致BottomNav也消失的情况,大概率是你误操作了整个AppBarLayout,或者布局联动的问题。试试这两种方案:

  • 方案一:只隐藏Toolbar本身
    别去动AppBarLayout,直接在代码里设置Toolbar的可见性就行:

    // 隐藏Toolbar
    findViewById<Toolbar>(R.id.toolbar).visibility = View.GONE
    

    这样只会把Toolbar藏起来,AppBarLayout和BottomNavigationView都不受影响,BottomNav会稳稳待在底部。

  • 方案二:调整布局结构彻底隔离联动
    如果你的需求是连AppBarLayout的空间都想去掉,又不想BottomNav跟着上移或隐藏,可以把BottomNavigationView从CoordinatorLayout里挪出来,用LinearLayout包裹整个页面:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">
    
            <!-- 这里放你的AppBarLayout和页面内容 -->
            <com.google.android.material.appbar.AppBarLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/Theme.MyApp.AppBarOverlay">
    
                <androidx.appcompat.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/Theme.MyApp.PopupOverlay"/>
    
            </com.google.android.material.appbar.AppBarLayout>
    
            <androidx.core.widget.NestedScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
                <!-- 你的页面内容 -->
    
            </androidx.core.widget.NestedScrollView>
    
        </androidx.coordinatorlayout.widget.CoordinatorLayout>
    
        <!-- BottomNav放在外面,完全不受CoordinatorLayout的联动影响 -->
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_nav"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?android:attr/windowBackground"
            app:menu="@menu/bottom_nav_menu"/>
    
    </LinearLayout>
    

    这样不管你怎么隐藏Toolbar或者AppBarLayout,BottomNav都会固定在底部,完全不受影响。

内容的提问来源于stack exchange,提问作者kvaruna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 08:31:33