【发布时间】: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