首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Compose BasicTextField的问题:

Compose BasicTextField的问题:
EN

Stack Overflow用户
提问于 2021-06-21 11:25:01
回答 1查看 275关注 0票数 0

现在我有了一个自定义的撰写输入框。BasicTextField,但我无论如何都不能居中输入字体。我只能通过Box的offset属性在x方向上进行偏移。

自定义输入框的原因是因为我需要一个固定的高度和字体大小的输入框。但是当高度不够时,不能输入文本字段。

请帮帮我,我更喜欢用TextField写固定高度的输入框。但是在网站上没有找到合适的解决方案。

下面是我的自定义代码:

代码语言:javascript
运行
复制
@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)
    )
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-21 16:10:58

您可以使用类似以下内容:

代码语言:javascript
运行
复制
BasicTextField(
    textStyle = LocalTextStyle.current.copy(textAlign = TextAlign.Center),
    decorationBox = {innerTextField ->
        Box(
            contentAlignment = Alignment.Center 
        ) {
            if (text.isEmpty()) {
                Text(
                    text = "placeHolder",
                )
            }
            innerTextField()
        }
    }
)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68061933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档