概述的“编辑文本”笔触颜色

我正在尝试设置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>
qinyong4004 回答:概述的“编辑文本”笔触颜色

您可以定义样式

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

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

更多info here

通过这种方式,您将以colorPrimary样式覆盖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的“全局”样式,该样式已经应用于清单中的整个应用程序

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

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

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

本文链接:https://www.f2er.com/2682773.html

大家都在问