如何在Jetpack Compose中将项目居中在表面内
提前感谢。
@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 中。如果我用图标替换文本,则可以完美居中。提前感谢。
fun CommonText(value : String, fontSize : TextUnit, color : Color, modifier : Modifier = Modifier, fontWeight : FontWeight = FontWeight.Normal, textAlign: TextAlign = TextAlign.Left) { Text(text = value, fontSize = fontSize, color = color, modifier = modifier) }
- Suman Radhakrishnan