Issue: change border color or box stroke for unfocused TextInputLayout in android

前端 未结 3 2091
醉话见心
醉话见心 2020-12-24 13:00

I have a very specific issue for changing the outline of the textbox for TextInputLayout when it\'s unfocused. I can\'t seem to find an attribute to change the color for the

相关标签:
3条回答
  • 2020-12-24 13:27

    If you want to set the color for the outline box in unfocused mode instead of the default black, you have to add this line in colors.xml file which will override the default color for the outline box.

    copy this line as it is. you can change color to what you want.

    <color name="mtrl_textinput_default_box_stroke_color">#fff</color>
    

    until now it will work, for more control on TextInputLayout you may add this style in styles.xml

    <style name="TextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
        <item name="boxStrokeColor">#fff</item>
        <item name="boxStrokeWidth">2dp</item>
    </style>
    

    then add theme to TextInputLayout

    android:theme="@style/TextInputLayoutStyle"
    
    0 讨论(0)
  • 2020-12-24 13:35

    Answer from Amjad was right, but starting from 1.1.0-alpha02 (probably even alpha01) after this issue was resolved it's possible to define color in color state list using the same boxStrokeColor attribute, e.g. how it's done in lib:

    <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>
    
    0 讨论(0)
  • 2020-12-24 13:43

    With the Material Components Library just use the boxStrokeColor attribute.
    It can work with a selector.

    Just use something like:

        <com.google.android.material.textfield.TextInputLayout
            app:boxStrokeColor="@color/text_input_layout_stroke_color"
            ..>
    

    with:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:alpha="..." android:color="@color/...." android:state_focused="true"/>
      <item android:alpha="..." android:color="@color/...." android:state_hovered="true"/>
      <item android:alpha="..." android:color="@color/...." android:state_enabled="false"/>
      <item android:alpha="..." android:color="@color/...."/>  <!-- unfocused -->
    </selector>
    

    0 讨论(0)
提交回复
热议问题