现在我有一个自定义的 Compose 输入框。 BasicTextField,但无论如何我都无法将输入字体居中。我只能通过Box的offset属性在x方向偏移。
自定义输入框的原因是因为我需要一个固定高度的输入框和字体大小。但是高度不够时无法输入文本框。
请帮帮我,我更喜欢用TextField来写固定高度的输入框。但是网站上没有找到合适的解决方案。
这是我的自定义代码:
@Composable
fun InputEditText(
value: String,
modifier: Modifier,
onValueChange: (String) -> Unit,
contentTextStyle: TextStyle,
hintTextStyle: TextStyle,
placeHolderString: String = "",
enabled: Boolean = true,
readOnly: Boolean = false,
singleLine: Boolean = false,
maxLines: Int = Int.MAX_VALUE,
offsetDp: Dp = 10.dp,
keyboardOptions: KeyboardOptions = KeyboardOptions.Default,
keyboardActions: KeyboardActions = KeyboardActions.Default,
cursorColor: Color = Color.Black,
) {
BasicTextField(
value = value,
onValueChange = onValueChange,
modifier = modifier,
textStyle = contentTextStyle,
decorationBox = {innerTextField ->
Box(
modifier = Modifier
.fillMaxWidth()
.offset(x = offsetDp),
contentAlignment = Alignment.CenterStart,
) {
if (value.isEmpty()) {
Text(
text = placeHolderString,
color = hintTextStyle.color,
fontSize = hintTextStyle.fontSize
)
}
innerTextField()
}
},
enabled = enabled,
readOnly = readOnly,
singleLine = singleLine,
maxLines = maxLines,
keyboardOptions = keyboardOptions,
keyboardActions = keyboardActions,
cursorBrush = SolidColor(cursorColor)
)
}