【发布时间】:2021-11-27 05:39:55
【问题描述】:
如何在喷气背包组合的表面内居中物品
@Composable
fun RoundedShapeWithIconCenter(
modifier: Modifier = Modifier,
parentSize : Dp,
parentBackgroundColor : Color,
childPadding : Dp,
icon : Painter,
iconSize : Dp,
iconTint : Color,
elevation : Dp = 0.dp,
isTextOrIcon : Boolean = false,
insideText : String = "",
insideTextColor : Color = colorResource(id = R.color.black),
fontSize: TextUnit = 16.sp
) {
Surface(
modifier = modifier.size(parentSize),
shape = RoundedCornerShape(50),
color = parentBackgroundColor,
elevation = elevation,
) {
if (isTextOrIcon) {
CommonText(value = insideText, fontSize = fontSize, color = insideTextColor, textAlign = TextAlign.Center)
} else {
Icon(painter = icon, contentDescription = "Back Arrow", modifier = Modifier
.size(iconSize)
.padding(childPadding), tint = iconTint)
}
}
}
在图像中,圆形黑色形状是 Surface,Go 是 Surface 内的文本。我想将 Go 文本居中放置在 Surface 中。如果我用图标替换文本,它会完美居中
提前致谢
【问题讨论】:
-
你能分享一下 CommonText 可组合,因为我对此有点困惑。
-
```@Composable fun CommonText(value : String, fontSize : TextUnit, color : Color, modifier : Modifier = Modifier, fontWeight: FontWeight = FontWeight.Normal, textAlign: TextAlign = TextAlign.Left) { 文本(文本 = 值,字体大小 = 字体大小,颜色 = 颜色,修饰符 = 修饰符)}
-
谢谢,我添加了一个更简单的版本,它不需要向 Text 添加修饰符,因为 Box 还提供了 contentAlignment 参数。
标签: android center android-jetpack-compose surface