【问题标题】:How to change color of icon of selected item in material drawer?如何更改材质抽屉中所选项目的图标颜色?
【发布时间】:2015-08-12 16:54:46
【问题描述】:

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

【问题讨论】:

    标签: android material-design materialdrawer


    【解决方案1】:

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

    方法是:

    .withSelectedIcon()
    

    .withSelectedText() 用于文本颜色

    【讨论】:

    • 迷你抽屉有没有办法像普通抽屉一样有高亮颜色?
    【解决方案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 更改为最新版本。

    【讨论】:

      【解决方案3】:

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

      .withIconTintingEnabled(true)

      在每个 DrawerItem 上。您应用的原色将用作色调。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-05-20
        • 2021-11-08
        • 2016-05-15
        • 2018-08-22
        • 1970-01-01
        • 2015-10-30
        • 1970-01-01
        相关资源
        最近更新 更多