如何更改材料抽屉中所选项目图标的颜色?

时间:2015-08-12 16:54:47

标签: android material-design materialdrawer

我使用Mike Penze的Material Drawer库https://github.com/mikepenz/MaterialDrawer。在示例中,所选项目的图标颜色会发生变化,但在我的应用中并没有变化。如何改变颜色?

3 个答案:

答案 0 :(得分:4)

当然,首先,您在代码中着色不同的图标,将有色的图标定义为突出显示项目时显示的内容。

方法是:

.withSelectedIcon()
文字颜色

.withSelectedText()

答案 1 :(得分: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>

如果你没有声明其中的一些(你可以删除任何行),那么库将只使用默认值。

您希望更改的颜色是第一个,&#34; material_drawer_primary&#34;您可以将其与您自己的原色相关联,或者只是将#ffffff放在@ color / primary现在的位置。

不要忘记将该库声明为应用程序的build.gradle文件中的依赖项。

compile ('com.mikepenz.materialdrawer:library:2.8.1@aar') {
    transitive = true
}

但将2.8.1更改为最新的版本。

答案 2 :(得分:0)

无需手动着色图标。只需调用方法:

.withIconTintingEnabled(true)

每个DrawerItem上的

。您应用的主要颜色将用作色调。