TextInputLayout框颜色

TextInputLayout框颜色

本文介绍了TextInputLayout框颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下xml元素,我可以将TextInputLayout框正确地涂成白色,但这仅是在单击它之后.初始颜色仍然是默认颜色.

Using the following xml element I can properly color the TextInputLayout box white but that's only after clicking it. The initial color is still default.

     <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:hintTextColor="@color/colorWhite"
        app:boxStrokeColor="@color/colorWhite"
        app:boxBackgroundColor="@color/colorPrimaryLight">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:imeOptions="actionSearch"
            android:inputType="textAutoComplete"
            android:outlineAmbientShadowColor="@color/colorWhite"
            android:textColor="@color/colorWhite"
            android:maxLines="1" />

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

有没有一种方法可以使框和提示文本着色,而不仅是在真正赋予TextInput焦点之后才应用?

Is there a way to color the box and hint text in a way that not only applies after actually giving the TextInput focus?

推荐答案

要更改 TextInputLayout 中的颜色,只需使用以下内容:

To change the color in the TextInputLayout just use something like:

  <style name="OutlinedBoxColor" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <!-- border color in OutlinedBox
    <item name="boxStrokeColor">@color/text_input_layout_stroke_color</item>

    <!-- The color of the label when it is collapsed and the text field is active -->
    <item name="hintTextColor">@color/singleColor</item>
    <!-- The color of the label in all other text field states (such as resting and disabled) -->
    <item name="android:textColorHint">@color/.....</item>
  </style>

TextInputLayout 处于焦点状态而未处于焦点状态时的结果

The result when the TextInputLayout is focused and not focused

您应该为这些颜色使用颜色选择器:

You should use a color selector for these colors:

对于 boxStrokeColor

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="?attr/colorPrimary" android:state_focused="true"/>
  <item android:alpha="0.87" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
  <item android:alpha="0.12" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
  <item android:alpha="0.38" android:color="?attr/colorOnSurface"/>
</selector>

对于 android:textColorHint

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:alpha="0.38" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
  <item android:alpha="0.6" android:color="?attr/colorOnSurface"/>
</selector>

这篇关于TextInputLayout框颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:22