【问题标题】:How to change color Toolbar icons?如何更改颜色工具栏图标?
【发布时间】:2021-07-19 01:37:51
【问题描述】:

我目前正在尝试通过主题更改工具栏中图标的颜色(我知道通过 kotlin 进行此操作的方法,但我有兴趣能够通过主题在整个应用程序上进行此操作)。尽管我阅读了关于这个主题和几个主题的google doc,但我无法更改这些图标的颜色。我正在使用 MaterialTheme

我定义主题和活动的清单:

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

    </application>

主题.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>

样式.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>

正如您所见,工具栏是由主题提供的,因此活动 xml 在这里并不相关。我的图标是矢量格式,通过 AndroidStudio 生成。我尝试了this solution 但不工作:/

【问题讨论】:

    标签: android material-design android-toolbar android-theme material-components-android


    【解决方案1】:

    solution linked in the question 有效,但您使用的是样式(style="..")而不是主题覆盖(android:theme="..")。

    <style name="Base.Theme.MyApp" parent="Base.Theme.MaterialThemeBuilder">
        <item name="toolbarStyle">@style/Widget.MyTheme.Toolbar</item>
    </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>
    

    在你的布局中使用MaterialToolbar:

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

    【讨论】:

    • 谢谢,3 点按钮是红色的,但不是其他保持默认颜色的操作按钮。你的怎么定义的?在我这边,它是一个带有白色填充颜色的矢量按钮,它保持白色
    • @filol 类似:&lt;vector android:tint="?attr/colorControlNormal" ../&gt;
    猜你喜欢
    • 2015-04-21
    • 1970-01-01
    • 2020-05-09
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多