聚焦时TextInputLayout下划线颜色不适合自定义颜色

时间:2019-10-08 06:38:42

标签: android android-textinputlayout material-components-android material-components textinputlayout

不知道我缺少什么,但是每次edittext聚焦时,下划线颜色都不适应我设置的自定义颜色。供参考的是我的主题代码

<style name="EditTextHintWhite" parent="@style/AppTheme">
   <item name="color">@color/white</item>
   <item name="android:textColorHint">@color/white</item>
   <item name="colorControlNormal">@color/white</item>
   <item name="colorControlActivated">@color/white</item>
   <item name="colorError">@color/white</item>
</style>

如您所见,我将它们全部设置为白色,但是当edittext聚焦时,下划线变为绿色

enter image description here

这是布局中的代码

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/EditTextHintWhite">

        <androidx.appcompat.widget.AppCompatEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/email"
            android:inputType="textEmailAddress"
            android:maxLines="1"
            android:shadowColor="@color/white"
            android:singleLine="true"
            android:textColor="@color/white"
            app:backgroundTint="@color/white" />

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

1 个答案:

答案 0 :(得分:0)

具有FilledBox样式的下划线颜色由 boxStrokeColor 属性定义。您可以在布局或自定义样式中添加它。像这样:

<style name="...." parent="Widget.MaterialComponents.TextInputLayout.FilledBox" >
    <!-- underline color in FilledBox style -->
    <item name="boxStrokeColor">@color/custom_selector_filled_stroke_color</item>
    ....
</style>

这是选择器的默认值:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:color="?attr/colorPrimary" android:state_focused="true"/>
  <!-- 4% overlay over 42% colorOnSurface -->
  <item android:alpha="0.46" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
  <item android:alpha="0.38" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
  <item android:alpha="0.42" android:color="?attr/colorOnSurface"/>
</selector>

聚焦时使用的颜色是第一行<item android:color="?attr/colorPrimary" android:state_focused="true"/>

enter image description here

在代码中使用(删除app:backgroundTint中的EditText

    <com.google.android.material.textfield.TextInputLayout
        android:hint="@string/email"
        android:theme="@style/EditTextHintWhite"
        ..>

        <com.google.android.material.textfield.TextInputEditText
            android:inputType="textEmailAddress"
            android:maxLines="1"
            android:textColor="@color/white"
            ../>

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

with(删除父母):

  <style name="EditTextHintWhite">
    <item name="colorError">@color/white</item>
    <item name="colorPrimary">@color/...</item>
    <item name="colorOnSurface">@color/...</item>
  </style>

如果您想使用自定义下划线,请在app:boxStrokeColor="@color/text_input_layout_stroke_color"中使用TextInputLayout属性。

注意:使用com.google.android.material.textfield.TextInputEditText代替androidx.appcompat.widget.AppCompatEditText