You need to enable JavaScript to run this app. Android中Fragment内ScrollView遮挡BottomNavigationView问题问询
优惠活动
大模型
产品
解决方案
定价
更多

Android中Fragment内ScrollView遮挡BottomNavigationView问题问询

问题根因

你的Activity布局中没有预留Fragment的专属承载容器,Fragment加载后会默认铺满整个CoordinatorLayout父布局,自然就覆盖了底部的导航栏。按以下步骤修改即可解决:


1. 修改Activity XML布局

在CoordinatorLayout中新增Fragment承载容器,同时配置避让底部导航栏的规则,不需要写死高度,适配性更强:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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/landing_page_body"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".pages.LandingPage"
    tools:openDrawer="start">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- 新增Fragment专属承载容器 -->
        <FrameLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_dodgeInsetEdges="bottom" />

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_navbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:transformPivotX="100dp"
            app:layout_insetEdge="bottom"
            app:labelVisibilityMode="unlabeled"
            app:menu="@menu/bottom_navbar_menu"
            app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"/>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/side_navbar_header"
        app:menu="@menu/side_navbar_menu" />

</androidx.drawerlayout.widget.DrawerLayout>

注:原导航栏的android:layout_alignParentBottom是RelativeLayout专属属性,在CoordinatorLayout中无效,可直接删除。


2. 修改Fragment加载逻辑

加载Fragment时指定承载容器为上面新增的fragment_container,不要直接添加到根布局或CoordinatorLayout中:

// Kotlin示例代码
supportFragmentManager.beginTransaction()
    .replace(R.id.fragment_container, 你的Fragment实例)
    .commit()
// Java示例代码
getSupportFragmentManager().beginTransaction()
    .replace(R.id.fragment_container, 你的Fragment实例)
    .commit();

3. 优化Fragment的ScrollView属性

修改ScrollView的高度属性,保证滚动体验正常,内容不足时也能撑满可用区域:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:isScrollContainer="false">

    <!-- 原有内部布局保持不变 -->

</ScrollView>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.19 19:15:04