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

Android小屏幕导航栏替代方案问题:navigationBarsHeight无法解析

解决Compose 1.5.1中navigationBarsHeight()无法解析的问题

在Compose 1.5.1版本中,navigationBarsHeight()这个Modifier扩展已被移除,取而代之的是基于WindowInsets的标准化API来处理系统栏高度。针对你的场景,提供两种可行修正方案:

方案一:用windowInsetsPadding适配系统导航栏占位

如果你的需求是在大屏幕上保留与底部导航栏对应的占位空间,同时自动适配系统导航栏的位置(避免内容被遮挡),可以直接使用windowInsetsPadding:

import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalConfiguration

@Composable
fun YourScreen() {
    val config = LocalConfiguration.current

    val useBottomNavigation by remember {
        derivedStateOf { config.smallestScreenWidthDp < 600 }
    }

    Scaffold(bottomBar = {
        if (useBottomNavigation) {
            BottomNavigationBar()
        } else {
            Spacer(
                Modifier
                    .fillMaxWidth()
                    .windowInsetsPadding(WindowInsets.navigationBars)
            )
        }
    }) { paddingValues ->
        // 屏幕内容,使用paddingValues避免被底部占位遮挡
    }
}

方案二:手动获取导航栏高度设置固定占位

如果需要精确设置与导航栏高度一致的固定占位空间,可以通过LocalDensity将WindowInsets的像素值转换为dp:

import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity

@Composable
fun YourScreen() {
    val config = LocalConfiguration.current
    val density = LocalDensity.current

    val useBottomNavigation by remember {
        derivedStateOf { config.smallestScreenWidthDp < 600 }
    }

    val navigationBarHeight = remember {
        with(density) {
            WindowInsets.navigationBars.getBottom(this).toDp()
        }
    }

    Scaffold(bottomBar = {
        if (useBottomNavigation) {
            BottomNavigationBar()
        } else {
            Spacer(
                Modifier
                    .fillMaxWidth()
                    .height(navigationBarHeight)
            )
        }
    }) { paddingValues ->
        // 屏幕内容
    }
}

关键说明

  • Compose 1.5.x开始统一使用WindowInsets API处理系统栏、状态栏等窗口内边距,旧的navigationBarsHeight()这类扩展方法已被废弃移除。
  • 方案一的windowInsetsPadding会自动响应导航栏的显示/隐藏状态,适配性更强;方案二更适合需要固定高度占位的场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 10:52:37