Android Compose中如何修改Box背景色并设置圆角?
给Box添加背景色和圆角的实现方案
直接修改Box的modifier参数就能实现需求,只需添加背景色与圆角形状的修饰符:
修改后的完整代码
Box( modifier = modifier .background( color = Color.LightGray, // 替换成你需要的背景色 shape = RoundedCornerShape(12.dp) // 替换成你想要的圆角尺寸 ) .padding(16.dp) // 可选:给内容和背景增加间隔,视觉更美观 ) { Row { Image( painter = painterResource(id = topic.image), contentDescription = stringResource(id = topic.name) ) Column( modifier = Modifier.padding(start = 8.dp) // 可选:给图片和文字添加横向间隔 ) { Text(text = stringResource(id = topic.name)) Text(text = stringResource(id = topic.pixel)) } } }
关键说明
RoundedCornerShape()支持多种写法:传单个数值时四个角圆角一致;也可以单独指定某几个角的圆角,比如RoundedCornerShape(topStart = 16.dp, bottomEnd = 16.dp)- 用
background(color, shape)可以一步完成背景设置与圆角裁剪;如果需要先裁剪内容再设置背景,也可以分开写:modifier.clip(RoundedCornerShape(12.dp)).background(Color.LightGray)
内容的提问来源于stack exchange,提问作者Ritu Suman Mohanty
相关产品推荐
相关产品推荐

