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

时间:2021-05-26 16:48:05

标签: android android-jetpack-compose android-compose-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!

2 个答案:

答案 0 :(得分:0)

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

类似于:

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

enter image description here

答案 1 :(得分:-2)

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

示例:

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