簡體   English   中英

如何更改顏色工具欄圖標?

[英]How to change color Toolbar icons?

我目前正在嘗試通過主題更改工具欄中圖標的 colors (我知道通過 kotlin 執行此操作的方法,但我有興趣能夠通過主題在整個應用程序上執行此操作)。 即使我閱讀了關於這個主題和幾個主題的谷歌文檔,我也無法更改這些圖標的顏色。 我正在使用 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>

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>

如您所見,工具欄由主題提供,因此活動 xml 在這里並不重要。 我的圖標是矢量格式,通過 AndroidStudio 生成。 我已經嘗試過這個解決方案但沒有工作:/

問題中鏈接的解決方案有效,但您使用的是樣式(帶有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
     .../>

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM