You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Jetpack Compose:如何让Scaffold内容铺满全屏并实现底部导航栏模糊透明效果

Jetpack Compose:如何让Scaffold内容铺满全屏并实现底部导航栏模糊透明效果

我来帮你搞定这两个问题!你的代码现在有两个核心问题:一是Scaffold的内容区域被自动添加了内边距,导致绿色背景没法铺满到屏幕最底部;二是底部栏用的是简单半透明背景,没有实现你要的模糊效果。下面分步骤修改:

一、让绿色背景铺满整个屏幕

默认情况下,Scaffold会给content区域自动加内边距来避开bottomBar,这就导致你的绿色内容被“挤”上去,没法延伸到底部栏下方。我们通过两个小修改就能解决:

  1. 给Scaffold设置contentWindowInsets = WindowInsets(0),关闭它自动添加的内边距;
  2. 去掉content里Box的padding(it),让背景直接填充整个屏幕空间。

修改后的MainMethod代码:

@Composable
fun MainMethod() {
    var selectedItemIndex by remember { mutableStateOf(0) }
    Scaffold(
        modifier = Modifier.fillMaxSize(),
        contentWindowInsets = WindowInsets(0), // 关闭自动内边距,让内容能铺满全屏
        bottomBar = { 
            BlurredBottomBar(selectedItemIndex,onItemSelected = { selectedItemIndex = it })
        },
        content = { 
            // 移除padding(it),让绿色背景直接覆盖整个屏幕
            Box(modifier = Modifier.fillMaxSize().background(color = Color.Green)) {
                Text("Main Content")
            } 
        }
    )
}

二、给底部导航栏添加模糊透明效果

原来的底部栏用Color.Black.copy(0.09f)作为背景,只是简单的半透明,没有真正的模糊效果。我们可以用Compose的BackdropFilter配合BlurEffect,让底部栏能模糊显示下方的绿色背景。

修改后的BlurredBottomBar代码:

@Composable
fun BlurredBottomBar(selectedItemIndex: Int, onItemSelected: (Int) -> Unit) {
    val iconList = IconLists()
    val itemList = iconList.itemList
    val selectedItemList = iconList.selectedItemList

    Box(
        modifier = Modifier
            .padding(start = 10.dp, end = 10.dp, bottom = 20.dp)
            .fillMaxWidth()
            .graphicsLayer(
                shadowElevation = 50f,
                shape = RoundedCornerShape(50.dp),
                clip = true
            )
            // 先加一层半透明底色,再叠加模糊效果
            .background(Color.Black.copy(0.15f))
            .backdropFilter(BlurEffect(blurRadius = 12.dp)) // 模糊半径,数值越大模糊程度越高
    ) {
        Row(
            modifier = Modifier
                .height(60.dp)
                .padding(10.dp)
                .fillMaxWidth(),
            verticalAlignment = Alignment.Bottom,
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            itemList.forEachIndexed { index, icon ->
                // 把var改成val更规范,因为这个值是只读的
                val iconResId = if (index == selectedItemIndex) selectedItemList[index] else itemList[index]
                IconButton(
                    modifier = if(selectedItemIndex == index) 
                        Modifier.background(
                            Color.White.copy(alpha = 0.25f),
                            shape = CircleShape
                        ) 
                    else Modifier,
                    onClick = { onItemSelected(index) }
                ) {
                    Icon(
                        painter = painterResource(id = iconResId),
                        contentDescription = "",
                        modifier = Modifier.size(25.dp),
                    )
                }
            }
        }
    }
}

额外小提示

  • 如果模糊效果不生效,检查下项目是否包含androidx.compose.ui:ui-graphics依赖(新建Compose项目默认会自带);
  • 模糊半径blurRadius和背景透明度0.15f都可以根据你的视觉需求调整,数值越大模糊/透明程度越高;
  • 我把代码里的几个var改成了val(比如itemListiconResId),因为这些变量不需要重新赋值,用只读变量更符合Kotlin的规范。

这样修改后,你的绿色背景就会铺满整个屏幕,底部导航栏也会呈现出模糊透明的效果,能清晰透过它看到下方的绿色啦!

内容来源于stack exchange

火山引擎 最新活动