无论如何,水平和垂直居中 TextField 中的文本?

Is there anyway to center text within a TextField horizontally and vertically?

在 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() })

)

转到 xml 文件 并为 EditText

设置 android:gravity="center"

示例:

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

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

类似于:

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