微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无论如何,是否可以水平和垂直地将 TextField 中的文本居中?

如何解决无论如何,是否可以水平和垂直地将 TextField 中的文本居中?

在 Android compose 中,无论如何要在 TextField 中居中文本?

    TextField(
    value = text,onValueChange = setText,maxLines = 2,modifier = modifier
        .clip(RoundedCornerShape(10.dp))
        .background(Color(0xffe2ebf0))
        .border(Borderstroke(5.dp,color = MaterialTheme.colors.secondary),shape = RoundedCornerShape(1.dp)),textStyle = MaterialTheme.typography.h3,keyboardOptions = KeyboardOptions.Default.copy(imeAction = ImeAction.Done),keyboardActions = KeyboardActions(onDone = { submit() })

)

Cannot post images yet... So here is a link!

解决方法

您可以在 textAlign = TextAlign.Center 中使用 TextStyle

类似于:

TextField(
    //...your code
    textStyle = MaterialTheme.typography.h3.copy(textAlign = TextAlign.Center),)

enter image description here

,

转到xml文件 并为 EditText 设置 android:gravity="center"

示例:

<EditText
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center"
    android:text="some text"
/>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。