[英]How to change color of icon of selected item in material drawer?
I use the Mike Penze's Material Drawer library https://github.com/mikepenz/MaterialDrawer . 我使用了Mike Penze的Material Drawer库https://github.com/mikepenz/MaterialDrawer 。 On samples the icon color of selected item changes, but in my app doesn't.
在样本上,所选项目的图标颜色会更改,但在我的应用程序中不会更改。 How to make color to change?
如何使颜色改变?
Sure, First you tint the different icons then in code, define the tinted ones as what shows when an item is highlighted. 当然,首先您要对不同的图标进行着色,然后在代码中将着色的图标定义为突出显示项目时显示的图标。
The method is: 方法是:
.withSelectedIcon()
and .withSelectedText()
for text color 和
.withSelectedText()
表示文本颜色
MaterialDrawer has a list of styles and colors that you can choose to change from their default values. MaterialDrawer包含样式和颜色的列表,您可以选择这些样式和颜色以更改其默认值。
Add this to your styles.xml 将此添加到您的styles.xml
<style name="CustomTheme" parent="MaterialDrawerTheme">
<!-- ...and here we setting appcompat’s color theming attrs -->
<item name="colorPrimary">@color/material_drawer_primary</item>
<item name="colorPrimaryDark">@color/material_drawer_primary_dark</item>
<item name="colorAccent">@color/material_drawer_accent</item>
<!-- MaterialDrawer specific values -->
<item name="material_drawer_background">@color/material_drawer_background</item>
<item name="material_drawer_icons">@color/material_drawer_icons</item>
<item name="material_drawer_primary_text">@color/material_drawer_primary_text</item>
<item name="material_drawer_primary_icon">@color/material_drawer_primary_icon</item>
<item name="material_drawer_secondary_text">@color/material_drawer_secondary_text</item>
<item name="material_drawer_hint_text">@color/material_drawer_hint_text</item>
<item name="material_drawer_divider">@color/material_drawer_divider</item>
<item name="material_drawer_selected">@color/material_drawer_selected</item>
<item name="material_drawer_selected_text">@color/material_drawer_selected_text</item>
<item name="material_drawer_header_selection_text">@color/material_drawer_header_selection_text</item>
</style>
Add this to your colors.xml 将此添加到您的colors.xml
<!-- Material Drawer -->
<!-- Material DEFAULT colors -->
<color name="material_drawer_primary">@color/primary</color>
<color name="material_drawer_primary_dark">@color/primary_dark</color>
<color name="material_drawer_primary_light">@color/primary_light</color>
<color name="material_drawer_accent">@color/accent</color>
<!-- OVERWRITE THESE COLORS FOR A LIGHT THEME -->
<!-- MaterialDrawer DEFAULT colors -->
<color name="material_drawer_background">#F9F9F9</color>
<!-- Material DEFAULT text / items colors -->
<color name="material_drawer_icons">#FFF</color>
<color name="material_drawer_primary_text">#DE000000</color>
<color name="material_drawer_primary_icon">#8A000000</color>
<color name="material_drawer_secondary_text">#8A000000</color>
<color name="material_drawer_hint_text">#42000000</color>
<color name="material_drawer_divider">#1F000000</color>
<!-- Material DEFAULT drawer colors -->
<color name="material_drawer_selected">#E8E8E8</color>
<color name="material_drawer_selected_text">@color/primary</color>
<color name="material_drawer_header_selection_text">#FFF</color>
<!-- OVERWRITE THESE COLORS FOR A DARK THEME -->
<!-- MaterialDrawer DEFAULT DARK colors -->
<color name="material_drawer_dark_background">#303030</color>
<!-- MaterialDrawer DEFAULT DARK text / items colors -->
<color name="material_drawer_dark_icons">#000</color>
<color name="material_drawer_dark_primary_text">#DEFFFFFF</color>
<color name="material_drawer_dark_primary_icon">#8AFFFFFF</color>
<color name="material_drawer_dark_secondary_text">#8AFFFFFF</color>
<color name="material_drawer_dark_hint_text">#42FFFFFF</color>
<color name="material_drawer_dark_divider">#1FFFFFFF</color>
<!-- MaterialDrawer DEFAULT DARK drawer colors -->
<color name="material_drawer_dark_selected">#202020</color>
<color name="material_drawer_dark_selected_text">@color/material_drawer_primary</color>
<color name="material_drawer_dark_header_selection_text">#FFF</color>
If you do not declare some of these (you can delete any of the lines), then the library will just use the default values. 如果不声明其中一些(可以删除任何行),则库将仅使用默认值。
The color you are looking to change is the first one, "material_drawer_primary" which you can either relate to your own primary color, or simply place #ffffff where @color/primary is right now. 您要更改的颜色是第一个颜色“ material_drawer_primary”,您可以将其与您自己的原色相关,也可以将#ffffff放置在@ color / primary现在所在的位置。
Don't forget to declare the library as a dependency in your app's build.gradle file. 不要忘记在应用程序的build.gradle文件中将库声明为依赖项。
compile ('com.mikepenz.materialdrawer:library:2.8.1@aar') {
transitive = true
}
but change 2.8.1 to whatever version is the most recent. 但请将2.8.1更改为最新版本。
There is no need to tint your icons manually. 无需手动着色图标。 Just call the method:
只需调用方法:
.withIconTintingEnabled(true)
on each DrawerItem. 在每个DrawerItem上。 Your app's primary colour will be used as the tint colour.
应用程序的原色将用作色彩。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.