Jetpack Compose 在 TextField 中对齐输入文本

问题描述

我想使用来自 Jetpack Compose 的 TextField 实现类似的行为,就像来自旧式 XML 布局一样:

<EditText
    android:id="@+id/some_id"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right" /> <!-- or end -->

当我尝试这种方法时:

TextField(value = "",onValueChange = {

},textAlign = TextAlign.End) 

它只是不起作用,因为 textAlign 属性TextField 中不存在。那么如何对TextAlign.Center进行TextAlign.EndTextAlign.JustifyTextField等输入文本对齐?

解决方法

您可以通过 textStyle 来完成。

TextField(
    value = "",onValueChange = {

    },textStyle = LocalTextStyle.current.copy(textAlign = TextAlign.End)
)

LocalTextStyle.currentTextField 的默认值,您可以自行替换。