如何在 Jetpack Compose 中将 Surface 中的 Item 居中
How to center Item inside Surface in jetpack compose
如何在 jet pack 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里面的Text。我想让 Go 文本在 Surface 中居中。如果我用图标替换文本,它会完美居中
提前致谢
为此,我们已将可组合文本对齐到中心,并且我们不能在 Surface 内使用对齐修饰符。所以我们将把我们的 CommonText 包裹在 Box 周围,并对接受修饰符的 CommonText 做一点改变。
RoundedShapeWithIconCenter
....
if (isTextOrIcon) {
Box(modifier = Modifier
.fillMaxSize(1.0f) // it will fill parent box
.padding(8.dp)) { // padding will help us to give some margin between our text and parent if text greater then our parent size
CommonText(
value = insideText,
fontSize = fontSize,
color = insideTextColor,
modifier = Modifier.align(Alignment.Center) // this will help it to align it to box center
)
}
}
....
修改后的 CommonText
as i don't know how CommonText Composable is created i assume it like following and make changes according to it.
@Composable
fun CommonText(modifier: Modifier = Modifier, .... ) {
Text(modifier = modifier, .... )
}
编辑 - 更简单的版本
....
if (isTextOrIcon) {
Box(modifier = Modifier
.fillMaxSize(1.0f) // it will fill parent box
.padding(8.dp),// padding will help us to give some margin between our text and parent if text greater then our parent size
contentAlignment = Center) { // contentAlignment will align its content as provided Alignment in our case it's Center
CommonText(
value = insideText,
fontSize = fontSize,
color = insideTextColor
)
}
}
....
如何在 jet pack 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里面的Text。我想让 Go 文本在 Surface 中居中。如果我用图标替换文本,它会完美居中
提前致谢
为此,我们已将可组合文本对齐到中心,并且我们不能在 Surface 内使用对齐修饰符。所以我们将把我们的 CommonText 包裹在 Box 周围,并对接受修饰符的 CommonText 做一点改变。
RoundedShapeWithIconCenter
....
if (isTextOrIcon) {
Box(modifier = Modifier
.fillMaxSize(1.0f) // it will fill parent box
.padding(8.dp)) { // padding will help us to give some margin between our text and parent if text greater then our parent size
CommonText(
value = insideText,
fontSize = fontSize,
color = insideTextColor,
modifier = Modifier.align(Alignment.Center) // this will help it to align it to box center
)
}
}
....
修改后的 CommonText
as i don't know how CommonText Composable is created i assume it like following and make changes according to it.
@Composable
fun CommonText(modifier: Modifier = Modifier, .... ) {
Text(modifier = modifier, .... )
}
编辑 - 更简单的版本
....
if (isTextOrIcon) {
Box(modifier = Modifier
.fillMaxSize(1.0f) // it will fill parent box
.padding(8.dp),// padding will help us to give some margin between our text and parent if text greater then our parent size
contentAlignment = Center) { // contentAlignment will align its content as provided Alignment in our case it's Center
CommonText(
value = insideText,
fontSize = fontSize,
color = insideTextColor
)
}
}
....