android - colorControlNormal 不适用于 MaterialToolbar 的图标

标签 android android-toolbar android-theme android-styles material-components-android

我想为我的 MaterialToolbar 设置主题,使其所有图标的颜色都解析为值 ?attr/colorOnPrimary (#FFF)。我不想必须直接在 XML 布局文件中设置 android:theme。相反,我希望能够覆盖 toolbarStyle 属性来控制主题(按照 Material Design page 中的建议)。

这些是我的应用使用的主题的相关属性。

<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 为可绘制对象设置色调。我在哪里弄错了主题,我该如何解决?

最佳答案

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

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

关于android - colorControlNormal 不适用于 MaterialToolbar 的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63173247/

相关文章:

android - 如何组合两个独立的android样式来创建一个新的

java - Android 通知未启动 Activity

android:自定义背景的最佳实践

android - 如何在工具栏上绘制带有抽屉导航和自定义图标的 Material 滑动标签?

java - 调用 onSharedPreferenceChanged 后应用主题没有改变

android - 如何通过 Robolectric 以编程方式获取自定义主题值

android - 无法找到在 Windows 上执行 "gradle assembleRelease"的位置

Android Dagger 依赖注入(inject)在私有(private)字段上失败

Android透明状态栏和 Action 栏

android - 如何在工具栏中添加图像