Fut*_*one 1 android android-view kotlin android-jetpack-compose
现在我有一个自定义的撰写输入框。BasicTextField,但无论如何我都可以 \xe2\x80\x99t 将输入字体居中。我只能通过Box的offset属性在x方向上进行偏移。
\n之所以要自定义输入框,是因为我需要一个固定高度的输入框和字体大小。但当高度不够时,文本字段无法输入。
\n请帮助我,我更喜欢使用 TextField 编写固定高度的输入框。但在网站上并没有找到合适的解决方案。
\n这是我的自定义代码:
\n@Composable\nfun InputEditText(\n value: String,\n modifier: Modifier,\n onValueChange: (String) -> Unit,\n contentTextStyle: TextStyle,\n hintTextStyle: TextStyle,\n placeHolderString: String = "",\n enabled: Boolean = true,\n readOnly: Boolean = false,\n singleLine: Boolean = false,\n maxLines: Int = Int.MAX_VALUE,\n offsetDp: Dp = 10.dp,\n keyboardOptions: KeyboardOptions = KeyboardOptions.Default,\n keyboardActions: KeyboardActions = KeyboardActions.Default,\n cursorColor: Color = Color.Black,\n) {\n BasicTextField(\n value = value,\n onValueChange = onValueChange,\n modifier = modifier,\n textStyle = contentTextStyle,\n decorationBox = {innerTextField ->\n Box(\n modifier = Modifier\n .fillMaxWidth()\n .offset(x = offsetDp),\n contentAlignment = Alignment.CenterStart,\n ) {\n if (value.isEmpty()) {\n Text(\n text = placeHolderString,\n color = hintTextStyle.color,\n fontSize = hintTextStyle.fontSize\n )\n }\n\n innerTextField()\n\n }\n },\n enabled = enabled,\n readOnly = readOnly,\n singleLine = singleLine,\n maxLines = maxLines,\n keyboardOptions = keyboardOptions,\n keyboardActions = keyboardActions,\n cursorBrush = SolidColor(cursorColor)\n )\n}\nRun Code Online (Sandbox Code Playgroud)\n
你可以使用类似的东西:
BasicTextField(
textStyle = LocalTextStyle.current.copy(textAlign = TextAlign.Center),
decorationBox = {innerTextField ->
Box(
contentAlignment = Alignment.Center
) {
if (text.isEmpty()) {
Text(
text = "placeHolder",
)
}
innerTextField()
}
}
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3404 次 |
| 最近记录: |