更改TextInputLayout轮廓颜色

Add*_*dev 37 android android-styles material-design android-textinputlayout

我正在尝试使用材质样式自定义TextInputLayout.我设法将聚焦状态设置为我想要的颜色:

在此输入图像描述

运用

<com.google.android.material.textfield.TextInputLayout
     style="@style/LoginTextInputLayoutStyle"
     android:theme="@style/LoginTextInputLayoutStyle"
     android:textColorHint="#fff"
     app:boxStrokeColor="#fff"
     .....>
          <EditText ...
Run Code Online (Sandbox Code Playgroud)

风格在哪里:

<style name="LoginTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
    <item name="colorAccent">#fff</item>
</style>   
Run Code Online (Sandbox Code Playgroud)

但是当textinput没有聚焦时,我得到了这样的表情:

在此输入图像描述

如何将黑线的颜色也改为白色.谢谢

Rut*_*att 82

使用此样式应用边框颜色和边框宽度,如下所示:

<style name="LoginTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
    <item name="boxStrokeColor">#fff</item>
    <item name="boxStrokeWidth">2dp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

从此链接获取有关样式的其他详细信息

colors.xml文件中添加以下行,以覆盖默认颜色TextInputLayout

<color name="mtrl_textinput_default_box_stroke_color" tools:override="true">#fff</color>
Run Code Online (Sandbox Code Playgroud)

  • @Addev检查我编辑的答案并在colors.xml文件中添加颜色. (4认同)
  • 这会在聚焦模式下更改轮廓,但是当编辑文本未聚焦=( (2认同)
  • @ParikshitChalke在xml中设置textColorHint属性 (2认同)
  • @RutvikBhatt 如果我更改了颜色的默认颜色,那么它会更改所有活动中的轮廓框颜色,但我想要不同活动的笔触颜色不同,那么该怎么办呢? (2认同)

kro*_*oot 21

从适用于Android的Material Components的1.1.0-alpha02开始,它可以简单地ColorStateList为这些项创建一个.程序如下:

res/color/text_input_box_stroke.xml插入如下内容:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#fcc" android:state_focused="true"/>
    <item android:color="#cfc" android:state_hovered="true"/>
    <item android:color="#ccf"/>
</selector>
Run Code Online (Sandbox Code Playgroud)

然后在你的styles.xml你会把:

<style name="LoginTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
    <item name="boxStrokeColor">@color/text_input_box_stroke</item>
</style>
Run Code Online (Sandbox Code Playgroud)

最后说明你的实际风格TextInputLayout:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/my_layout_id"
    style="@style/LoginTextInputLayoutStyle"
    ...
Run Code Online (Sandbox Code Playgroud)

  • 可以确认,适用于1.1.0-alpha08 (3认同)

小智 5

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#FFFFFF"/>

<item android:state_focused="false" android:color="#FFFFFF"/></selector>
Run Code Online (Sandbox Code Playgroud)

创建颜色目录并在其中创建资源文件,将上述代码粘贴到颜色目录 xml 文件中,并在文本输入布局样式中粘贴以下行

<item name="boxStrokeColor">@color/focus_tint_list</item>
Run Code Online (Sandbox Code Playgroud)


Adr*_*zin 5

从材料组件Alpha 7开始,您只需按以下方式创建颜色选择器文件:colors / text_input_outline_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:color="@color/buttonDark"/>
    <item android:state_hovered="true" android:color="@color/buttonDark"/>
    <item android:state_focused="true" android:color="@color/buttonDark"/>
    <item android:color="@color/buttonDark"/>
</selector>
Run Code Online (Sandbox Code Playgroud)

有关如何设置的更多信息。这是相关的源代码:

ColorStateList boxStrokeColorStateList =
    MaterialResources.getColorStateList(context, a, R.styleable.TextInputLayout_boxStrokeColor);
if (boxStrokeColorStateList != null && boxStrokeColorStateList.isStateful()) {
  defaultStrokeColor = boxStrokeColorStateList.getDefaultColor();
  disabledColor =
      boxStrokeColorStateList.getColorForState(new int[] {-android.R.attr.state_enabled}, -1);
  hoveredStrokeColor =
      boxStrokeColorStateList.getColorForState(new int[] {android.R.attr.state_hovered}, -1);
  focusedStrokeColor =
      boxStrokeColorStateList.getColorForState(new int[] {android.R.attr.state_focused}, -1);
} else {
  // If attribute boxStrokeColor is not a color state list but only a single value, its value
  // will be applied to the box's focus state.
  focusedStrokeColor =
      a.getColor(R.styleable.TextInputLayout_boxStrokeColor, Color.TRANSPARENT);
  defaultStrokeColor =
      ContextCompat.getColor(context, R.color.mtrl_textinput_default_box_stroke_color);
  disabledColor = ContextCompat.getColor(context, R.color.mtrl_textinput_disabled_color);
  hoveredStrokeColor =
      ContextCompat.getColor(context, R.color.mtrl_textinput_hovered_box_stroke_color);
}
Run Code Online (Sandbox Code Playgroud)

从此列表中可以看到,您要确保使用的是已定义所有状态的颜色选择器,否则它将默认返回另一种颜色。


小智 5

材料编辑文本

步骤1:在build.gradle(Module App)模块依赖部分添加库

实现 'com.android.support:design:28.0.0-alpha1'

第 2 步:xml 代码

<com.google.android.material.textfield.TextInputLayout
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:boxStrokeColor="#0000FF"
            app:boxStrokeWidth="2dp"
            android:layout_gravity="center"
            >

           <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/txtusername"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/lable" />

        </com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)


Ali*_*yed 5

首先从 TextInputLayout 中删除

<item name="boxStrokeColor">@color/YourColor</item>
Run Code Online (Sandbox Code Playgroud)

二、添加新的颜色属性

 <color name="mtrl_textinput_default_box_stroke_color" tools:override="true" >YourColor</color>
Run Code Online (Sandbox Code Playgroud)

一定要写同名 mtrl_textinput_default_box_lines_color不要