如何更改TextInputLayout提示文本的背景颜色

时间:2020-03-15 01:30:39

标签: android material-design android-textinputlayout

我正在编写TextInputLayout的视图

我的style.xml是:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="MyTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
        <item name="boxStrokeColor">@color/colorPrimary</item>
        <item name="hintTextColor">@color/colorPrimary</item>
        <item name="boxBackgroundColor">@android:color/white</item>
        <item name="boxStrokeWidth">2dp</item>
    </style>
</resources>

我在color.xml中的颜色替代为:

<color name="mtrl_textinput_default_box_stroke_color" tools:override="true">#c2261a</color>

我的TextInput布局是:

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/weightInput"
        style="@style/MyTextInputLayoutStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="15dp"
        android:hint="@string/hint_weight"
        app:errorEnabled="true"
        app:helperTextEnabled="false"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textViewTitle">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:imeOptions="actionDone"
            android:importantForAutofill="no"
            android:inputType="numberDecimal"
            android:maxLength="5"
            android:maxLines="1"
            android:singleLine="true"
            android:textAlignment="center"
            android:textColor="@color/colorPrimary" />

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

最终结果是

final result

我们可以看到提示文本的背景颜色是透明的。如何找到该视图。

我尝试过这种风格但没有成功:

我的style.xml是:

<resources>

  <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="HintText" parent="TextAppearance.Design.Hint">
        <item name="android:textColor">@color/colorPrimary</item>
        <item name="android:background">@android:color/white</item>
    </style>

    <style name="MyTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
        <item name="boxStrokeColor">@color/colorPrimary</item>
        <item name="hintTextColor">@color/colorPrimary</item>
        <item name="hintTextAppearance">@style/HintText</item>
        <item name="boxBackgroundColor">@android:color/white</item>
        <item name="boxStrokeWidth">2dp</item>
    </style>
</resources>

3 个答案:

答案 0 :(得分:3)

更新到材质组件 1.4.0-alpha01 解决了这个问题。 这是负责的提交:https://github.com/material-components/material-components-android/commit/6015a4e901dc55a02f86e12703820d520684f95e

答案 1 :(得分:-1)

它对我有用。我希望它对你也有用。

<EditText
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:textSize="14sp" 
    android:hint="Add Your Text Watermark"
    android:textColorHint="@color/yellowcolor" // this line can change hint text color
    android:textColor="@color/yellowcolor"
    android:fontFamily="@font/inter_light"
    android:textAlignment="center"/>

答案 2 :(得分:-2)

在TextInputEditText中使用backgroundTint

android:backgroundTint="@color/colorPrimaryDark"

示例代码:

<com.google.android.material.textfield.TextInputEditText
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:backgroundTint="@color/colorPrimaryDark"
                    android:imeOptions="actionNext"
                    tools:text="Full name" />