无论如何,是否可以水平和垂直地将 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"
/>