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

问题描述

我正试图更改com.google.android.material.textfield.TextInputLayout的框笔触颜色,但没有聚焦,但是没有属性,这是我的TextInputLayout代码

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>

,我搜索了一种更改其颜色的方法,并找到了此链接: https://github.com/material-components/material-components- android/issues/112

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>

这解决了问题并更改了笔触框的颜色,但是这里的问题是我想在同一应用程序的其他TextInputLayouts中更改此颜色!

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

推荐答案

要更改框的笔触颜色,只需使用xml中的 app:boxStrokeColor 属性即可.

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>

这篇关于在TextInputLayout.OutlinedBox中自定义边界框描边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 17:12