colorControlNormal未应用于MaterialToolbar的图标。

3
我想设置我的MaterialToolbar主题,使其所有图标的颜色都解析为?attr/colorOnPrimary (#FFF)的值。我不想在XML布局文件中直接设置android:theme。相反,我希望能够覆盖toolbarStyle属性来控制主题(根据 Material Design页面的建议)。
这些是我的应用程序使用的主题相关属性。
<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更改为其他颜色值,溢出图标确实会根据提供给属性的值进行更改,但我的另一个图标完全没有受到影响。这是显示内容。

enter image description here

供参考,这是用于填充工具栏菜单的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>
我尝试过并且有效的方法:将SVG的path android:fillColor更改为?attr/colorControlNormal。理想情况下,我不想篡改SVG的fillColor属性,只想通过colorControlNormal为可绘制对象设置色调。我在主题方面做错了什么,如何修复?
1个回答

3

您可以在您的向量中使用:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    ..
    android:tint="?attr/colorControlNormal">
       ....
</vector>

enter image description here

只是一个提示:在你的样式中可以使用:

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

colorControlNormal为什么不能给矢量图着色,而我必须直接着色矢量图?(Material Design页面指定,可以通过主题设置来调整操作图标的颜色,而不是直接操作SVG文件。) - Richard
“到您的提示”。但是我通过我的自定义ThemeOverlay更改了colorControlHighlight。如果我不至少扩展原始样式(即ThemeOverlay.MaterialComponents.Toolbar.Primary),我无法自定义colorControlHighlight。您是指将该样式作为父级扩展到我的自定义样式中吗? - Richard
在这种情况下,在 ThemeOverlay.GithubUser.Toolbar 中只需设置 parent ="ThemeOverlay.MaterialComponents.Toolbar.Primary" - Gabriele Mariotti
很长,无法在评论中解释。但是你有检查溢出图标吗?它具有 android:tint="?attr/colorControlNormal" - Gabriele Mariotti
@Richard,你可以通过Android Studio浏览样式,查找actionOverflowButtonStyle。或者在这里检查源代码:https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev/appcompat/appcompat/src/main/res/drawable/abc_ic_menu_overflow_material.xml - Gabriele Mariotti
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接