简体   繁体   English

如何在 Jetpack Compose 中将 Surface 中的 Item 居中

[英]How to center Item inside Surface in jetpack compose

How to center item inside the surface in jet pack 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)
        }
    }
}


在此处输入图像描述

In image the circular black color shape is Surface and Go is Text inside Surface.在图像中,圆形黑色形状是表面,Go 是表面内的文本。 I want to center the Go text inside the Surface.我想将 Go 文本置于 Surface 的中心。 If I replace text with icon it center perfectly如果我用图标替换文本,它会完美居中

Thanks in advance提前致谢

for this we have align our Text composable to the centre, and we can't use align modifier inside Surface.为此,我们已将可组合文本对齐到中心,并且我们不能在 Surface 内使用对齐修饰符。 so we will wrap our CommonText around Box and make a little change to CommonText that accept modifier.所以我们将把我们的 CommonText 包裹在 Box 周围,并对接受修饰符的 CommonText 做一点改变。

RoundedShapeWithIconCenter 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
            )
        }
}
....

Modified CommonText修改后的通用文本

as i don't know how CommonText Composable is created i assume it like following and make changes according to it.因为我不知道 CommonText Composable 是如何创建的,所以我假设它喜欢跟随并根据它进行更改。

@Composable
fun CommonText(modifier: Modifier = Modifier, .... ) {
    Text(modifier = modifier, .... )
}

Edit - easier version编辑 - 更简单的版本

....
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
            )
        }
}
....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM