如何在使用新的 prefixText 时更改 TextInputLayout 的提示文本填充?

5
我尝试使用新的prefixText实现TextInputLayout,并使用com.google.android.material:material:1.2.0-alpha02。这是一个非常酷的功能,但是当我添加一个前缀文本时,提示标签会浮动并与前缀对齐。这看起来不太好,特别是在同一页上有更多没有前缀的输入字段时,浮动标签无法对齐。

screenshot

我的布局代码的相关部分:

 <LinearLayout
       android:id="@+id/login_input_fields"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/login_username_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/username_hint"
        app:prefixText="Prefix">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/login_username_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:singleLine="true" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/login_password_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/default_margin"
        android:imeOptions="actionDone"
        app:endIconMode="password_toggle">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/password_hint"
            android:inputType="textPassword"
            android:singleLine="true"/>

    </com.google.android.material.textfield.TextInputLayout>
</LinearLayout>

我无法重现1.2.0-alpha02版本的问题。 - Gabriele Mariotti
非常奇怪,Gabriele。可能会有其他依赖项或其他因素影响到此问题。我也在使用1.2.0-alpha02版本。 - Gober
你是否会以编程方式更改类似“gravity”这样的东西?或者在应用程序主题中使用“textInputStyle”属性定义自定义样式? - Gabriele Mariotti
现在我尝试在Android Studio中使用空白活动模板创建一个全新的项目。添加了上面的布局代码和material:1.2.0-alpha02,甚至能够在预览窗口中重现它(您必须向TextInputEditText添加文本以使提示标签浮起)。 - Gober
好的,我能够复制这个问题。这取决于应用程序中使用的错误主题。请查看下面的答案。 - Gabriele Mariotti
2个回答

5
要使用Material Components Library,您必须使用Theme.MaterialComponents.*主题
使用此主题使用您的布局:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
 ...
</style>

enter image description here

使用此主题的布局:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
  ...
</style>

enter image description here


2
 val prefixView = textInputLayout.findViewById<AppCompatTextView>(com.google.android.material.R.id.textinput_prefix_text)
  prefixView.layoutParams = LinearLayout.LayoutParams(
      ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT)
  prefixView.gravity = Gravity.CENTER

如果你想在输入文本前加亮前缀,可以在这里找到更多细节 https://github.com/material-components/material-components-android/issues/773


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接