android - colorControlNormal 不适用于 MaterialToolbar 的图标
问题描述
我想为我的 MaterialToolbar 设置主题,以便将其所有图标的颜色解析为值?attr/colorOnPrimary
(#FFF)。我不想android:theme
直接在 XML 布局文件中设置。相反,我希望能够覆盖toolbarStyle
属性来控制主题(根据材料设计页面中的建议)。
这些是我的应用程序使用的主题的相关属性。
<style name="Base.Theme.GithubUser" parent="Theme.MaterialComponents.DayNight.NoActionBar">
...
<item name="colorOnPrimary">@color/white</item>
<item name="toolbarStyle">@style/Widget.GithubUser.Toolbar</item> <!-- I want to control all the styles (themes) of my MaterialToolbar through this attribute -->
</style>
我使用的样式(@style/Widget.GithubUser.Toolbar
)如下。
<style name="Widget.GithubUser.Toolbar" parent="Widget.MaterialComponents.Toolbar.Primary">
...
<item name="materialThemeOverlay">@style/ThemeOverlay.GithubUser.Toolbar</item>
</style>
<style name="ThemeOverlay.GithubUser.Toolbar" parent="">
<item name="colorControlNormal">?attr/colorOnPrimary</item>
<item name="colorControlHighlight">@color/material_toolbar_icon_highlight_color</item>
</style>
我现在拥有的是:我的溢出图标确实有色,?attr/colorOnPrimary
但我的其他图标不受影响。我尝试更改colorControlNormal
为其他颜色值,并且溢出图标确实根据提供给属性的值而更改,但我的其他图标根本不受影响。这是显示器。
作为参考,这是我menu.xml
用来扩充工具栏菜单的文件。
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_navigate_to_favourite_fragment"
android:title="@string/show_favorites_text"
android:icon="@drawable/drawable_favorite"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_change_settings"
android:title="@string/change_language_text"
app:showAsAction="never"/>
</menu>
错误显示的图标是@drawable/drawable_favorite
SVG 文件。其内容如下。
<vector android:height="24dp" android:viewportHeight="412.735"
android:viewportWidth="412.735" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M295.706,35.522C295.706,35.522 295.706,35.522 295.706,35.522c-34.43,-0.184 -67.161,14.937 -89.339,41.273c-22.039,-26.516 -54.861,-41.68 -89.339,-41.273C52.395,35.522 0,87.917 0,152.55C0,263.31 193.306,371.456 201.143,375.636c3.162,2.113 7.286,2.113 10.449,0c7.837,-4.18 201.143,-110.759 201.143,-223.086C412.735,87.917 360.339,35.522 295.706,35.522z"/>
</vector>
我已经尝试和工作:将path android:fillColor
SVG 更改为?attr/colorControlNormal
. 理想情况下,我不想篡改 SVGfillColor
属性,而只是能够通过colorControlNormal
. 我在哪里弄错了主题,我该如何解决?
解决方案
您可以在向量中使用:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
..
android:tint="?attr/colorControlNormal">
....
</vector>
只是一个提示:在您的风格中,您可以使用:
<style name="Widget.GithubUser.Toolbar" parent="Widget.MaterialComponents.Toolbar.Primary">
<item name="materialThemeOverlay">@style/ThemeOverlay.MaterialComponents.Toolbar.Primary</item>
</style>
或者,如果您想扩展它,只需使用:
<style name="Widget.GithubUser.Toolbar" parent="Widget.MaterialComponents.Toolbar.Primary">
<item name="materialThemeOverlay">@style/ThemeOverlay.GithubUser.Toolbar</item>
</style>
和:
<style name="ThemeOverlay.GithubUser.Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
<item name="colorControlHighlight">@color/material_toolbar_icon_highlight_color</item>
</style>
推荐阅读
- r - 当字符串中有多个句点时, parse_number 是否应该失败?
- python - Pycharm2018 中的 pip 版本
- django - django rest框架自定义序列化器
- asp.net-core - 为什么连接/令牌端点在刷新identityServer4中的访问令牌时不验证刷新令牌
- android - 如何在谷歌地图的标记中添加多个位图图标?
- junit - 如何验证它是最后一个调用的方法
- php - Laravel 比较多个多对多关系
- c# - Log4Net 正在创建文件但未写入文件
- php - Laravel 中带属性的最小值和最大值
- node.js - 我的 express.js hello world 和 Docker 设置有什么问题?