gpt4 book ai didi

android - Jetpack Compose 文本字段标签不居中

转载 作者:行者123 更新时间:2023-12-05 04:43:59 26 4
gpt4 key购买 nike

我正在尝试制作一个非常小的文本字段 (32.dp)。执行此操作时,标签会被推到底部,我找不到保持其居中的方法?我试过使用 TextAlign.center 但没有成功

TextField(
modifier = Modifier
.height(32.dp)
.padding(horizontal = 8.dp)
.border(1.dp, colorResource(id = R.color.mono4), RoundedCornerShape(32.dp))
.constrainAs(commentBox) {
top.linkTo(parent.top)
bottom.linkTo(parent.bottom)
start.linkTo(profilePhoto.end)
end.linkTo(postText.start)
width = Dimension.fillToConstraints
height = Dimension.fillToConstraints
},
label = {
Text(
textAlign = TextAlign.Center,
text = "Comment on workout...",
style = TextStyle(
fontSize = 14.sp,
color = colorResource(id = R.color.mono4),
fontFamily = FontFamily(Font(R.font.josefin_san_regular))
)
)
},
colors = TextFieldDefaults.textFieldColors(
backgroundColor = Color.Transparent,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent
),
value = commentText,
onValueChange = { commentText = it }
)

enter image description here

最佳答案

这是内部行为,您需要更改源代码来修复它,或者实现您自己的可组合项,例如:-

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun TinyTextCapture(){
var value by remember { mutableStateOf("") }
Box(contentAlignment = Alignment.Center){
TextField(
modifier = Modifier.height(32.dp),
value = value, onValueChange = { value = it }
)
AnimatedVisibility(visible = value.isEmpty()) {
Text("Label")
}
}

}

默认标签参数有一个从顶部开始的默认填充,这就是它被视为向下推的原因。如果我是你,我不会去编辑像 TextField 这样基本的东西的源代码,因为它有太多的内部依赖性,以至于一团糟。

您也可以不使用 AnimatedVisibility,并应用 ConstraintLayout 以在 TextField 的开头获取它现在的绝对居中位置。

Works like a charm

关于android - Jetpack Compose 文本字段标签不居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69456059/

26 4 0