Jetpack Compose 对齐 TextField 中的输入文本

Ars*_*ius 27 user-interface user-experience textfield text-alignment android-jetpack-compose

我想通过TextFieldJetpack Compose 实现类似的行为,就像旧式 XML 布局一样:

<EditText
    android:id="@+id/some_id"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="right" /> <!-- or end -->
Run Code Online (Sandbox Code Playgroud)

当我尝试这种方法时:

TextField(value = "", onValueChange = {

}, textAlign = TextAlign.End) 
Run Code Online (Sandbox Code Playgroud)

它根本不起作用,textAlign因为TextField. 那么如何对输入的文本进行对齐TextAlign.Center,如TextAlign.EndTextAlign.Justify等等TextField

Phi*_*hov 59

你可以通过textStyle.

TextField(
    value = "",
    onValueChange = {

    },
    textStyle = LocalTextStyle.current.copy(textAlign = TextAlign.End)
)
Run Code Online (Sandbox Code Playgroud)

LocalTextStyle.current是默认值TextField,您可以自行替换。

  • 在“文本”中,它称为“样式”。我不确定为什么采用这样的设计,可能是因为不需要经常更改“TextField”的对齐方式。您可以按照与“TextField”相同的方式为“Text”设置“textAlign”,但它还有“textAlign”来覆盖样式值。 (2认同)