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

How to change hint padding for TextInputLayout when using the new prefixText?

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

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

 <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>

要使用 Material 组件库,您 必须使用 一个 Theme.MaterialComponents.* theme.

将您的布局与此主题一起使用:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
 ...
</style>

将您的布局与此主题一起使用:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
  ...
</style>

 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 如果你想点亮带有输入文本的前缀