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

Android 如何将按钮文本居中对齐(Jetpack Compose),android,button,android-jetpack-compose,Android,Button,Android Jetpack Compose,我正在尝试将按钮的文本垂直和水平对齐到中心,默认情况下不在那里。 我曾尝试使用“偏移”来定位按钮中的文本,但不同大小的设备的定位并不一致 我的按钮的代码是: Button( onClick = { navController.navigate("fourth_screen") }, modifier = Mod

我正在尝试将按钮的文本垂直和水平对齐到中心,默认情况下不在那里。 我曾尝试使用“偏移”来定位按钮中的文本,但不同大小的设备的定位并不一致

我的按钮的代码是:

                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

            }

如何垂直和水平居中按钮中适用于所有设备尺寸的文本。

您只需使用修改器
align
将可组合文件居中:

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

        }