首页 > 解决方案 > 概述的编辑文本描边颜色

问题描述

我正在尝试设置 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>

标签: androidmaterial-designandroid-themeandroid-stylesandroid-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>

并将其应用于视图:

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

通过这种方式,您可以修改该视图和任何子视图的主题属性,这对于覆盖界面特定部分中的主题调色板很有用。

更多信息在这里

这样,您将覆盖colorPrimarystyle 中的属性Widget.MaterialComponents.TextInputLayout.OutlinedBox

例如,它是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"
   ..>

我希望所有样式为 OutlinedBox 的项目都将框设为绿色“?我想避免为每个视图重复主题和样式...我的意思是从 AppTheme 继承的“全局”样式,它已经应用于清单中的整个应用程序

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

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

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


推荐阅读