customize Bounding box stroke in TextInputLayout.OutlinedBox

痴心易碎 提交于 2019-12-05 00:51:37

问题


I'm trying to change box Stroke Color of com.google.android.material.textfield.TextInputLayout while not focused but there is no attribute for that this is my TextInputLayout code

<com.google.android.material.textfield.TextInputLayout
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="36dp"
    android:layout_marginTop="38dp"
    android:layout_marginEnd="36dp"
    android:textColorHint="@color/white"
    app:boxStrokeColor="@color/white"
    app:boxStrokeWidth="1dp">

    <EditText
        android:id="@+id/editTxt_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_email"
        android:inputType="textEmailAddress"
        android:textColorHint="@color/white" />
</com.google.android.material.textfield.TextInputLayout>

and I searched for a way to change its color and found this link : https://github.com/material-components/material-components-android/issues/112

so i tried using this line in my colors file

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

this solved the problem and changed stroke box color but the issue here is that i want to change this color in other TextInputLayouts in the same app !!


回答1:


To change the box stroke color just use the app:boxStrokeColor attribute in the xml.

 <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        app:boxStrokeColor="@color/mySelector"
        ../>

You should use a selector. It is the default:

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

You can also use a custom style:

  <style name="MyOutlinedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="boxStrokeColor">@color/text_input_selector</item>
  </style>



来源:https://stackoverflow.com/questions/55210983/customize-bounding-box-stroke-in-textinputlayout-outlinedbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!