简体   繁体   English

如何更改颜色工具栏图标?

[英]How to change color Toolbar icons?

I am currently trying to change the colors of the icons in the toolbar via the theme (I know the way to do it via kotlin but I am interested to be able to do it on the whole app via a theme).我目前正在尝试通过主题更改工具栏中图标的 colors (我知道通过 kotlin 执行此操作的方法,但我有兴趣能够通过主题在整个应用程序上执行此操作)。 Even though I read the google doc on this subject and several topics, I was not able to change the color of these icons.即使我阅读了关于这个主题和几个主题的谷歌文档,我也无法更改这些图标的颜色。 I'm using MaterialTheme我正在使用 MaterialTheme

Manifest where I defined my theme and my activity:清单我定义了我的主题和我的活动:

<application
        android:name=".App"
        android:theme="@style/Theme.MyApp">
        <activity
            android:name=".views.activity"
            android:label="@string/title_activity" />

    </application>

Themes.xml主题.xml

 <!--Top level DayNight theme to be used in AndroidManifest.xml-->
    <style name="Theme.MyApp" parent="Base.Theme.MyApp" />
    <!--Base custom theme which will be shared between both light and dark theme variants-->
    <style name="Base.Theme.MyApp" parent="Base.Theme.MaterialThemeBuilder">

        <!--Material color attributes (light theme) -->
        <!--colorPrimary colors map to components and elements, such as app bars and buttons. -->
        <!--colorSecondary colors are most often used as accents on components, such as FABs and -->
        <!--selection controls.-->
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryVariant">@color/primaryLightColor</item>
        <item name="colorSecondary">@color/secondaryColor</item>
        <item name="colorSecondaryVariant">@color/secondaryLightColor</item>

        <!--colorBackground appears behind scrollable content and is used for the default window-->
        <!--background. colorSurface is mapped to the surface of components such as cards, sheets-->
        <!--and menus. colorError is used to indicate an error state for components such as-->
        <!--text fields.-->
        <item name="android:colorBackground">@color/grey_200</item>
        <item name="colorSurface">@color/black_800</item>
        <item name="colorError">@color/red_600</item>
        <!--"On" colors define how text, icons and strokes are colored in relation to the surface-->
        <!--on which they appear.-->
        <item name="colorOnPrimary">@color/black_800</item>
        <item name="colorOnSecondary">@color/black_800</item>
        <item name="colorOnBackground">@color/black_800</item>
        <item name="colorOnSurface">@color/black_800</item>
        <item name="colorOnError">@color/white</item>
        <!--Material type attributes-->
        ...
        <!--Material shape attributes-->
        ...
        <item name="toolbarStyle">@style/Widget.MyTheme.Toolbar</item>
    </style>

Styles.xml Styles.xml

    <!--Toolbar-->
    <style name="Widget.MyTheme.Toolbar" parent="Widget.MaterialComponents.Toolbar.Surface"> // I have also try ThemeOVerlay here and Toolbar.Primary
        <item name="android:background">@color/grey_200</item>
        <item name="titleTextAppearance">@style/TextAppearance.MyTheme.Headline6</item>
        <item name="subtitleTextAppearance">@style/TextAppearance.MyTheme.Subtitle1</item>
        <item name="titleTextColor">@color/black_800</item>
        <!-- color used by navigation icon and overflow icon -->
        <item name="colorOnPrimary">@color/red_800</item>
        <item name="colorControlNormal">@color/red_800</item>
    </style>

As you can see the toolbar is provided by the theme so the activity xml is not revelant here.如您所见,工具栏由主题提供,因此活动 xml 在这里并不重要。 My icons are in vector format, generated via AndroidStudio.我的图标是矢量格式,通过 AndroidStudio 生成。 I have try this solution but not working:/我已经尝试过这个解决方案但没有工作:/

The solution linked in the question works but you are using a style (with style=".." ) not a theme overlay (with android:theme=".." ).问题中链接的解决方案有效,但您使用的是样式(带有style=".." )而不是主题覆盖(带有android:theme=".." )。

<style name="Base.Theme.MyApp" parent="Base.Theme.MaterialThemeBuilder">
    <item name="toolbarStyle">@style/Widget.MyTheme.Toolbar</item>
</style>

With the theme attribute toolbarStyle you are using a style :使用主题属性toolbarStyle样式,您正在使用样式

<style name="Widget.MyTheme.Toolbar" parent="Widget.MaterialComponents.Toolbar">
    <!-- Title text color -->
    <item name="titleTextColor">@color/colorSecondary</item>
    <!-- ..... -->

    <!-- ThemeOverlay -->
    <item name="materialThemeOverlay">@style/MyThemeOverlay_Toolbar</item>
</style>

<style name="MyThemeOverlay_Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
    <!-- color used by navigation icon and overflow icon -->
    <item name="colorOnPrimary">@color/red600Dark</item>
</style>

and in your layout use a MaterialToolbar :并在您的布局中使用MaterialToolbar

    <com.google.android.material.appbar.MaterialToolbar
     .../>

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在折叠时更改工具栏图标颜色 - How to change toolbar icons color on collapsing 如何更改工具栏中图标的颜色? - How can I change the color of icons in Toolbar? 如何在Android Studio的Platte工具栏中更改图标颜色 - How to change the Icons color in the Platte toolbar in Android Studio 更改材质工具栏中溢出菜单图标的颜色 - Change the color of overflow menu icons in a material toolbar 如何在没有样式工具栏的情况下动态更改所有ToolBar的图标颜色 - How to change all ToolBar's icons color dynamically without styling toolbar 如何更改工具栏颜色 - How to change toolbar color 如何更改上面工具栏的颜色? - How to change the color of above Toolbar? 如何更改工具栏高程颜色? - How to Change Toolbar Elevation Color? 如何以编程方式为工具栏内的所有图标设置 colorControlHighlight (波纹颜色)? - How to set colorControlHighlight (ripple color) for all the icons inside toolbar programmatically? 我应该如何使用材料设计更改顶部工具栏中图标和标题的颜色? -NoActionBar 主题 - Android 材料设计 - How should I change the color of the icons and title in the top toolbar using material design? -NoActionBar theme - Android Material Design
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM