如何更改androidx工具栏中的菜单图标颜色



我得到了一个带有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声明中更改它,但都不起作用。

有人能帮我吗?

对于材料组件主题,您可以使用:

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

如果使用MaterialToolBar,则可以在"活动"中使用此代码:

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

最新更新