【问题标题】:How to change menu icon color in androidx toolbar?如何更改androidx工具栏中的菜单图标颜色?
【发布时间】:2026-01-01 22:30:01
【问题描述】:

我得到一个带有 androidx.appcompat.widget.Toolbar 的布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    tools:context=".Activity.Home">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintTop_toTopOf="parent"
        android:backgroundTint="#4862ed"
        app:titleTextColor="#ffff"/>

</androidx.constraintlayout.widget.ConstraintLayout>

所以我想更改工具栏中设置的三点菜单图标的颜色,但是我没有找到任何可以正常工作的解决方案。

我尝试定义一个新样式,从活动中设置或在 xml 声明中更改它,但没有任何效果。

有人可以帮助我吗?

【问题讨论】:

    标签: android android-toolbar androidx android-theme


    【解决方案1】:

    您可以使用材质组件主题:

    <androidx.appcompat.widget.Toolbar
        android:theme="@style/ThemeOverlay.App.Toolbar"
        ...>
    
      <style name="ThemeOverlay.App.Toolbar" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary">
        <!-- color used by navigation icon and overflow icon -->
        <item name="colorOnPrimary">@color/myColor</item>
      </style>
    

    使用 AppCompat 主题:

    <androidx.appcompat.widget.Toolbar
      app:theme="@style/ThemeOverlay.App.Toolbar" />
    
    
    <style name="ThemeOverlay.App.Toolbar" parent="Theme.AppCompat.Light">
    
       <!-- navigation icon color -->
       <item name="colorControlNormal">@color/my_color</item>
    
        <!-- color of the menu overflow icon -->
        <item name="android:textColorSecondary">@color/my_color</item>
    </style>
    

    【讨论】:

      【解决方案2】:

      如果您使用 MaterialToolBar,您可以在 Activity 中使用此代码:

      MaterialToolBarId.menu.findItem(R.id.yourItemId).icon.setTint(resources.getColor(R.color.yourColor))
      

      【讨论】:

        最近更新 更多