简体   繁体   English

如何居中对齐按钮文本(Jetpack Compose)

[英]How to center align a button text (Jetpack Compose)

I'm trying to align the text of my button to the center vertically and horizontally, which isn't there by default.我正在尝试将按钮的文本垂直和水平对齐到中心,默认情况下不存在。 I have tried to use 'offset' to position text in my button but the positioning isn't consistent across various device sizes.我尝试在我的按钮中对 position 文本使用“偏移”,但不同设备尺寸的定位不一致。

The code for my button is:我的按钮的代码是:

                Button(
                onClick = {
                    navController.navigate("fourth_screen")
                },
                modifier = Modifier.constrainAs(buttonSave) {
                    top.linkTo(glButtonSaveTop)
                    bottom.linkTo(glButtonSaveBottom)
                    start.linkTo(glLeft)
                    end.linkTo(glRight)
                    width = Dimension.fillToConstraints
                    height = Dimension.fillToConstraints

                },
                enabled = !errorMsg.value,
                colors = if (query.value.text != ""){
                    ButtonDefaults.
                    buttonColors(backgroundColor = colorResource(id = R.color.voodlee_red))}
            else {
                    ButtonDefaults.
                    buttonColors(backgroundColor = colorResource(id = R.color.gray))}

            ) {
                Text(
                    "Save", color = colorResource(id = R.color.dark_blue),
                    fontSize = with(LocalDensity.current) {
                        dimensionResource(id = R.dimen._16ssp).toSp()
                    },
                    fontFamily = FontFamily(Font(R.font.poppins_medium)),
                    textAlign = TextAlign.Center,
                    modifier = Modifier.fillMaxSize().offset(y= (0.15).dp)) //offset for positioning

            }

How do I center the text in my button vertically and horizontally that works on all device sizes.如何使按钮中的文本垂直和水平居中,适用于所有设备尺寸。

EDIT: Any solution for this in stable Jetpack Compose?编辑:在稳定的 Jetpack Compose 中有什么解决方案吗?

You can just use the modifier align to center a Composable:您可以只使用修饰符align将 Composable 居中:

Button(
            onClick = {
                navController.navigate("fourth_screen")
            },
            modifier = Modifier.constrainAs(buttonSave) {
                top.linkTo(glButtonSaveTop)
                bottom.linkTo(glButtonSaveBottom)
                start.linkTo(glLeft)
                end.linkTo(glRight)
                width = Dimension.fillToConstraints
                height = Dimension.fillToConstraints

            },
            enabled = !errorMsg.value,
            colors = if (query.value.text != ""){
                ButtonDefaults.
                buttonColors(backgroundColor = colorResource(id = R.color.voodlee_red))}
        else {
                ButtonDefaults.
                buttonColors(backgroundColor = colorResource(id = R.color.gray))}

        ) {
            Text(
                "Save", color = colorResource(id = R.color.dark_blue),
                fontSize = with(LocalDensity.current) {
                    dimensionResource(id = R.dimen._16ssp).toSp()
                },
                fontFamily = FontFamily(Font(R.font.poppins_medium)),
                textAlign = TextAlign.Center,  // horizontal center of the text
                modifier = Modifier.align(Alignment.CenterVertically) //vertical center of the Text composable

        }

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

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