我该如何改变汉堡菜单图标的颜色?

4
首先,我想澄清的是,我愿意更改汉堡包导航菜单图标本身的颜色,而不是导航菜单中的图标。
我按照以下教程进行操作:https://developer.android.com/training/implementing-navigation/nav-drawer#DrawerButton 结果,我在应用栏中有一个NavMenu图标(汉堡包)。问题:该图标为黑色(矢量可绘制对象的默认颜色)。
我创建了一个新样式:
<!-- Hamburger menu -->
<style name="MyDrawerArrowToggle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="color">@color/colorTextTitle</item>
</style>

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

<style name="customTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- Hamburger menu -->
    <item name="drawerArrowStyle">@style/MyDrawerArrowToggle</item>
</style>

确保在清单文件中使用了这种样式:

<application>
    android:theme="@style/customTheme"
</application>

还将此主题应用于工具栏(以防万一...)

<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>

操作结果:以上所有操作都没有产生任何影响。汉堡图标仍然是黑色的。

请问有人能够向我解释我犯了什么错误并告诉我如何改变这个颜色吗?

2个回答

6
我建议您看一下Google/Android Studio提供的示例。
  1. 创建一个名为test-hamburger的新项目(名称可选;-))
  2. 显然选择“抽屉式”模板。我没有选择“使用AndroidX”,但应该可以工作。
  3. 我选择了MinAPI 23/Target 28。

在您拥有示例应用程序之后,运行它并观察工具栏是绿色的,文本/色调是白色的。

打开values/styles.xml(不是v21版本,避免那些麻烦):)

这就是现有主题的外观:

    <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>

你需要添加以下代码到中: <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>

总之,你的样式现在应该看起来像:

    <!-- 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>

当运行时,它看起来像:

运行中


1
谢谢您提供这么详细的答案。我刚刚尝试了一下,它确实有效。然后我又使用API lvl 21(我的项目中使用的版本)进行了一次尝试,仍然有效(至少我知道那不是问题所在)。我会继续研究这个汉堡项目,并尝试找到正确的方法来实现我的主题。 - Eric
确保在样式中使用正确的父级,这非常重要:parent="@style/Widget.AppCompat.DrawerArrowToggle"parent="Widget.AppCompat.DrawerArrowToggle"不同。 - Martin Marconcini

0

使用此样式作为您的工具栏

<style name="Toolbar">
    <item name="android:textColorPrimary">@color/colorAccent</item>
    <item name="android:textColor">@color/colorAccent</item>
    <item name="android:textColorSecondaryInverse">@color/colorAccent</item>
    <item name="android:textColorSecondary">@color/colorAccent</item>
</style>

希望它有所帮助


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接