Spa*_*tta 1 android button android-jetpack-compose
我试图将按钮的文本垂直和水平居中对齐,默认情况下不存在。我尝试使用“偏移”来定位按钮中的文本,但在各种设备尺寸上定位并不一致。
我的按钮的代码是:
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
}
Run Code Online (Sandbox Code Playgroud)
如何使按钮中的文本垂直和水平居中,以适应所有设备尺寸。
编辑:在稳定的 Jetpack Compose 中是否有解决方案?
小智 5
您可以使用修饰符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
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10206 次 |
最近记录: |