android - 概述的编辑文本描边颜色
问题描述
我正在尝试设置 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"
..>
通过这种方式,您可以修改该视图和任何子视图的主题属性,这对于覆盖界面特定部分中的主题调色板很有用。
更多信息在这里。
这样,您将覆盖colorPrimary
style 中的属性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
材料组件库的版本。
推荐阅读
- python - 如何使用 tkinter 将回调绑定到列表框?
- android - 我的发布请求的结果是用户的身份验证令牌。我想在应用程序的 TextView 中显示此令牌,但 textview 没有更新
- spartacus-storefront - 多站点场景主题化?
- scala - Spark / Scala:根据当前行中的值变化将行拆分为多行
- pandas - PANDAS:数据框中按其他列分组的列的总和值
- python - 如何在 Python 中计算 .csv 中的项目数?
- c# - Unity C#中文件下载完成后如何调用方法?
- android - 为什么我不能在 onCreate 之外显示 toast 消息?
- javascript - JavaScript 中的正则表达式以匹配没有单词边界的书写系统
- json - 获取某个级别的json数据的key