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

Android中BottomNavigationViewEx菜单重叠内容的解决方法

How to Fix BottomNavigationViewEx Overlapping ScrollView Content

The key here is to let the Android layout system handle spacing automatically instead of guessing hardcoded heights—this ensures consistency across all device sizes and configurations. Here are the most reliable solutions, ordered by preference:

1. Use ConstraintLayout for Automatic Alignment

This is the cleanest and most maintainable approach. By constraining your ScrollView to sit directly above the BottomNavigationViewEx, the system will automatically calculate the correct height for both views without overlap.

Example layout code:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:id="@+id/scroll_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/bottom_nav"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <!-- Your ScrollView content goes here -->

    </ScrollView>

    <com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
        android:id="@+id/bottom_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <!-- Your bottom menu items -->

    </com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx>

</androidx.constraintlayout.widget.ConstraintLayout>

Setting the ScrollView's height to 0dp and pinning its bottom to the top of the nav bar ensures it only occupies the space above the menu, no overlap included.

2. Use LinearLayout with Weight

If you prefer LinearLayout over ConstraintLayout, leverage the layout_weight attribute to make the ScrollView fill all available vertical space except what's needed for the bottom nav.

Example layout:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <!-- Your ScrollView content -->

    </ScrollView>

    <com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- Your bottom menu items -->

    </com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx>

</LinearLayout>

The layout_weight="1" tells the ScrollView to expand to fill all remaining vertical space, while the bottom nav stays at its natural height (wrap_content).

3. Dynamically Calculate Nav Height (If Margins Are Necessary)

If you absolutely need to set a bottom margin on the ScrollView, you can get the actual height of the BottomNavigationViewEx after it's been laid out, then apply that as the margin.

Add this code to your Activity/Fragment:

val bottomNav = findViewById<com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx>(R.id.bottom_nav)
val scrollView = findViewById<ScrollView>(R.id.scroll_view)

bottomNav.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
    override fun onGlobalLayout() {
        // Remove the listener to avoid repeated calls
        bottomNav.viewTreeObserver.removeOnGlobalLayoutListener(this)
        
        // Get the nav bar's actual height
        val navHeight = bottomNav.height
        
        // Apply the height as bottom margin to the ScrollView
        val layoutParams = scrollView.layoutParams as ViewGroup.MarginLayoutParams
        layoutParams.bottomMargin = navHeight
        scrollView.layoutParams = layoutParams
    }
})

This waits until the nav bar is drawn, grabs its real height, and applies it as the margin. Note this is less ideal than the layout-based solutions, as it can cause a slight layout jump and requires extra code.

Quick Note on Default Height

Per Material Design guidelines, the standard height for BottomNavigationView (and BottomNavigationViewEx) is 56dp, but this can change if you customize the nav bar's text size, icon size, or padding. Relying on the layout system to handle spacing is always better than hardcoding this value—it adapts to customizations and device differences automatically.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 04:21:58