代码之家  ›  专栏  ›  技术社区  ›  Addev

更改文本输入布局大纲颜色

  •  26
  • Addev  · 技术社区  · 8 年前

    我正在尝试使用材质样式自定义文本输入布局。我成功地将聚焦状态设置为所需的颜色:

    enter image description here

    使用

    <com.google.android.material.textfield.TextInputLayout
         style="@style/LoginTextInputLayoutStyle"
         android:theme="@style/LoginTextInputLayoutStyle"
         android:textColorHint="#fff"
         app:boxStrokeColor="#fff"
         .....>
              <EditText ...
    

    其中样式为:

    <style name="LoginTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
        <item name="colorAccent">#fff</item>
    </style>   
    

    但当文本输入没有聚焦时,我会看到:

    enter image description here

    我怎么能把黑线的颜色也改成白色呢?谢谢

    5 回复  |  直到 7 年前
        1
  •  72
  •   Rutvik Bhatt    7 年前

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

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

    从中获取有关样式的其他详细信息 link

    在您的 colors.xml 覆盖默认颜色的文件 TextInputLayout

    <color name="mtrl_textinput_default_box_stroke_color" tools:override="true">#fff</color>
    
        2
  •  17
  •   kroot    7 年前

    截至版本 1.1.0-alpha02 在android的材料组件中,它可以简单地创建 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>
    

    然后在你 styles.xml 你会说:

    <style name="LoginTextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense">
        <item name="boxStrokeColor">@color/text_input_box_stroke</item>
    </style>
    

    最后指出你的风格 TextInputLayout :

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/my_layout_id"
        style="@style/LoginTextInputLayoutStyle"
        ...
    
        3
  •  3
  •   Adrian Le Roy Devezin    7 年前

    从材质组件Alpha 7开始,您只需创建一个颜色选择器文件,如下所示: 颜色/文本输入轮廓颜色.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>
    

    了解更多关于这是如何设置的上下文。以下是相关的源代码:

    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);
    }
    

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

        4
  •  2
  •   Monali    8 年前

    我创建了一个默认配置。

     <style name="Widget.Design.TextInputLayout" parent="AppTheme">
        <item name="hintTextAppearance">@style/AppTheme.TextFloatLabelAppearance</item>
        <item name="errorTextAppearance">@style/AppTheme.TextErrorAppearance</item>
        <item name="counterTextAppearance">@style/TextAppearance.Design.Counter</item>
        <item name="counterOverflowTextAppearance">@style/TextAppearance.Design.Counter.Overflow</item>
    </style>
    
    <style name="AppTheme.TextFloatLabelAppearance" parent="TextAppearance.Design.Hint">
        <!-- Floating label appearance here -->
        <item name="android:textColor">@color/colorAccent</item>
        <item name="android:textSize">20sp</item>
    </style>
    
    <style name="AppTheme.TextErrorAppearance" parent="TextAppearance.Design.Error">
        <!-- Error message appearance here -->
        <item name="android:textColor">#ff0000</item>
        <item name="android:textSize">20sp</item>
    </style>
    
        5
  •  0
  •   Uday Sai Kumar    7 年前
    <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>
    

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

    <item name="boxStrokeColor">@color/focus_tint_list</item>