轮廓编辑文本描边颜色

Outlined Edit Text stroke color

我正在尝试设置 TextInputLayout 的样式:

<style name="AppTheme.TextInputLayout.OutlinedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="boxStrokeColor">@color/text_input_layout_outlined_box_stroke</item>
    <item name="hintTextColor">@color/text_input_layout_outlined_box_stroke</item>
</style>

这就是颜色选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/green_2" android:state_focused="true" />
    <item android:color="@color/green_2" android:state_hovered="true" />
    <item android:color="@color/green_2" android:state_enabled="false" />
    <item android:color="@color/green_2" />
</selector>

这就是我的观点:

<com.google.android.material.textfield.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:hint="@string/surname">

    <com.google.android.material.textfield.TextInputEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</com.google.android.material.textfield.TextInputLayout>

为什么这会按预期应用于视图:

style="@style/AppTheme.TextInputLayout.OutlinedBox"

主题不工作:

android:theme="@style/AppTheme.TextInputLayout.OutlinedBox"

我不明白这两者之间的区别...

编辑:也许我发现这个是为了避免对每个视图重复:

<item name="textInputStyle">@style/AppTheme.TextInputLayout.OutlinedBox</item>

您可以定义样式

<style name="AppTheme.TextInputLayout.OutlinedBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="boxStrokeColor">@color/text_input_layout_outlined_box_stroke</item>
    <item name="hintTextColor">@color/text_input_layout_outlined_box_stroke</item>
</style>

并将其应用于视图:

<com.google.android.material.textfield.TextInputLayout
   style="@style/AppTheme.TextInputLayout.OutlinedBox"
   ..>

同时你可以定义:

  <style name="textInputPrimaryColor" parent="">
    <item name="colorPrimary">@color/.....</item>
  </style>

然后将其与 android:theme 属性一起使用:

<com.google.android.material.textfield.TextInputLayout
   style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
   android:theme="@style/textInputPrimaryColor"
   ..>

In this way you can modify the theme attributes for that view and any child views, which is useful for overriding theme color palettes in a specific portion of your interface.

更多info here.

通过这种方式,您将覆盖样式 Widget.MaterialComponents.TextInputLayout.OutlinedBox 中的 colorPrimary 属性。

例如它是 boxStrokeColor.

使用的默认选择器
<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>

使用 android:theme="@style/textInputPrimaryColor" 您可以更改此视图的 colorPrimary 而无需扩展样式。

您可以使用样式中的 materialThemeOverlay 属性实现相同的行为:

  <style name="My.OutlinedBox" parent="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="materialThemeOverlay">@style/ThemeOverlay.My.OutlinedBox</item>
  </style>

与:

  <style name="ThemeOverlay.My.OutlinedBox" parent="ThemeOverlay.MaterialComponents.TextInputEditText.OutlinedBox">
    <item name="colorPrimary">@color/......</item>
  </style>

然后将其应用到您的视图中:

<com.google.android.material.textfield.TextInputLayout
   style="@style/My.OutlinedBox"
   ..>

I want all my items with style OutlinedBox to have the box green colored"? I'd like to avoid repeating theme and style for every view...I mean a "global" style that inherit from AppTheme, which is already applied to the whole application in the manifest

目前没有一个属性可以为具有 OutlinedBox 样式的 TextInputLayout 定义样式。
您只能使用应用主题中的 textInputStyle 属性为应用中的所有 TextInputLayout 视图分配全局样式:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
   ...
   <item name="textInputStyle">@style/My.OutlinedBox</item>
</style>

注意: 它需要 Material 组件库的 1.1.0 版本。