[英]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.