如何更改汉堡菜单图标的颜色?

Eri*_*ric 4 android hamburger-menu

首先,我想澄清一下,我愿意更改汉堡导航菜单图标本身的颜色,而不是导航菜单中的图标。

我遵循了本教程:https://developer.android.com/training/implementing-navigation/nav-drawer#DrawerButton

结果,我在应用程序栏中有一个导航菜单图标(汉堡包)。问题:图标为黑色(Vector 可绘制的默认颜色)。

我创建了一种新样式:

<!-- Hamburger menu -->
<style name="MyDrawerArrowToggle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="color">@color/colorTextTitle</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后我将此样式添加到我的主题中:

<style name="customTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- Hamburger menu -->
    <item name="drawerArrowStyle">@style/MyDrawerArrowToggle</item>
</style>
Run Code Online (Sandbox Code Playgroud)

确保这种样式是我的应用程序在清单中使用的样式:

<application>
    android:theme="@style/customTheme"
</application>
Run Code Online (Sandbox Code Playgroud)

并且还将这个主题应用到工具栏(以防万一......)

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorToolbarBackground"
            app:theme="@style/customTheme"
            app:popupTheme="@style/customTheme"
            app:title="@string/app_name"
            app:titleTextColor="@color/colorTextBody">

        </android.support.v7.widget.Toolbar>
    </FrameLayout>
Run Code Online (Sandbox Code Playgroud)

操作结果:这些都没有任何效果。汉堡包图标仍然是极其黑色的。

你们中的任何人都可以向我解释我犯了什么错误以及如何改变这种颜色吗?

Mar*_*ini 6

我建议您看一下 Google/Android Studio 提供的示例。

  1. 创建一个名为test-hamburger(名称可选;-))的新项目
  2. 显然选择“抽屉”模板。我没有选中“使用 AndroidX”,但应该可以。
  3. 我选择了 MinAPI 23/Target 28。

获得示例应用程序后,运行它并观察工具栏为绿色,文本/色调为白色。

打开values/styles.xml(不是 v21 版本,f**c 那些):)

现有主题如下所示:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

您需要添加这一行: <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>

当然,定义样式:

    <style name="DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@android:color/holo_red_dark</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

总而言之,您的风格现在应该如下所示:

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    </style>

    <style name="DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@android:color/holo_red_dark</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

运行时,看起来像:

跑步