我使用 Mike Penze 的 Material Drawer 库https://github.com/mikepenz/MaterialDrawer。在示例中,所选项目的图标颜色会发生变化,但在我的应用程序中不会。如何让颜色发生变化?
问问题
2330 次
3 回答
4
当然,首先您为不同的图标着色,然后在代码中,将着色的图标定义为突出显示项目时显示的内容。
方法是:
.withSelectedIcon()
和.withSelectedText()
文字颜色
于 2015-08-27T17:27:41.570 回答
2
MaterialDrawer 有一个样式和颜色列表,您可以选择更改它们的默认值。
将此添加到您的 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>
将此添加到您的 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>
如果你没有声明其中的一些(你可以删除任何行),那么库将只使用默认值。
您要更改的颜色是第一个“material_drawer_primary”,您可以将其与您自己的原色相关联,或者只需将#ffffff 放在@color/primary 现在所在的位置。
不要忘记在应用程序的 build.gradle 文件中将库声明为依赖项。
compile ('com.mikepenz.materialdrawer:library:2.8.1@aar') {
transitive = true
}
但将 2.8.1 更改为最新版本。
于 2015-09-03T01:37:36.567 回答
0
无需手动为您的图标着色。只需调用方法:
.withIconTintingEnabled(true)
在每个 DrawerItem 上。您的应用程序的原色将用作色调颜色。
于 2017-08-08T17:24:00.927 回答