【发布时间】:2026-02-04 17:15:01
【问题描述】:
我正在尝试将按钮的文本垂直和水平对齐到中心,默认情况下不存在。 我尝试使用“偏移”在按钮中定位文本,但不同设备尺寸的定位不一致。
我的按钮代码是:
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
}
如何使按钮中的文本垂直和水平居中,适用于所有设备尺寸。
编辑:在稳定的 Jetpack Compose 中有什么解决方案吗?
【问题讨论】:
标签: android button android-jetpack-compose