如何设置MenuItem的图标颜色?

68

我定义了一个具有ShareActionProvider和共享白色图标的菜单项,如下所示:

<item
    android:icon="@drawable/ic_share_white_24dp"
    android:id="@+id/action_share"
    android:title="@string/action_share"
    android:orderInCategory="200"
    app:showAsAction="ifRoom"
    app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>

但是,当我启动应用程序时,我得到了一个不同的黑色分享图标。如何将分享图标设置为白色?

这是我的结果

enter image description here


应用程序:iconTint =“@color/yourcolor” - Ashraf Amin
9个回答

74

据我所知,图标实际上是由ShareActionProvider提供的,您无法更改它。但是,您可以通过在styles.xml中设置textColorPrimary来自定义颜色:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:theme="@style/MyActionBarTheme"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

<style name="MyActionBarTheme" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">#fa0</item>
</style>

对于任何自定义的图标,您都需要自己着色。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main, menu);

    for(int i = 0; i < menu.size(); i++){
        Drawable drawable = menu.getItem(i).getIcon();
        if(drawable != null) {
            drawable.mutate();
            drawable.setColorFilter(getResources().getColor(R.color.textColorPrimary), PorterDuff.Mode.SRC_ATOP);
        }
    }

    return true;
}

@YoannHercouet 你是想改变其他图标的颜色而不是ShareActionProvider图标吗?因为这个问题特别询问了它。你正在使用Toolbar吗? - tachyonflux
问题指定了MenuItems,但是我还是尝试使用ShareActionProvider。我发现这个图标会自动设置,所以你不需要手动设置图标,只需要像BladeCoder解释的那样更改主题,它就会自动显示为白色。我尝试了像你说的设置 textColorPrimary ,但它并没有改变任何东西,即使它能够改变,也不仅仅会改变ActionBar或ToolBar,而且还会影响应用程序中的所有标题和许多组件。 - Yoann Hercouet
@YoannHercouet 嗯,有些事情可能已经改变了,但如果工具栏被设计为它,ShareActionProvider和溢出菜单仍然受到textColorPrimary的影响。对于任何自定义图标,您必须自己着色。 - tachyonflux

68

简短明了的回答——> app:iconTint="@color/yourcolor

在你的MenuItem中加入app:iconTint="@color/yourcolor"以更改图标颜色。

<item
    android:icon="@drawable/ic_share_white_24dp"
    android:id="@+id/action_share"
    android:title="@string/action_share"
    android:orderInCategory="200"
    app:iconTint="@color/yourcolor"
    app:showAsAction="ifRoom"
    app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>

3
如果您想从menu-XML重新着色,这不是一个坏的解决方案:命名空间应该是_android:而不是_app: android:iconTint="@android:color/white"。重要提示:属性iconTint仅在API级别26及更高版本中使用! - Danny
32
请使用app:而不是android:,这样它将在API 26以下的版本中使用。 - Nikunj Paradva
4
没错,使用app:iconTitle甚至在API 26以下也能正常工作-谢谢!也许人们会感到困惑,因为在我的IDE预览中它没有正确显示,但在运行时正确着色!您的解决方案非常好,不应该被点赞踩! - Danny
注意,我之前使用了系统图标,并使用iconTint设置为“#FFFFFF”,但它仍然是灰色的。最终我创建了一个新的矢量图标来获得完全白色的图标。 - Joshua Pinter
1
谢谢@NikunjParadva,它像我想要的那样工作,点赞! - Ashraf Amin

48

这是一个主题问题。根据您当前的主题,您需要设置正确的ActionBar覆盖主题。操作提供程序读取主题中的值(指示主题是否为暗或亮)以确定图标的颜色。

如果您的主题为浅色,则ActionBar/工具栏必须使用主题ThemeOverlay.AppCompat.Dark.ActionBar以实现暗色ActionBar。


φàëφ‰·δΗΜιΔ‰Theme.AppCompat.Light.NoActionBarοΦ¨φàëη·ΞεΠ²δΫïε°ûγéΑε°ÉοΦü - Dimitri
5
确实,你的主题是光线,并且你正在使用工具栏。你需要在XML布局中的Toolbar上指定android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"属性。如果你希望工具栏弹出菜单是浅色的而工具栏是深色的,还需要添加app:popupTheme="@style/ThemeOverlay.AppCompat.Light" - BladeCoder
如果我设置主题为android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar",他会改变我的工具栏颜色吗? - Dimitri
2
这只会改变溢出图标的颜色,而不是菜单项的颜色。 - Yoann Hercouet
1
你可以在这里找到许多黑白颜色的图标:https://github.com/google/material-design-icons。 - stefan.m
显示剩余2条评论

42

试一下这个:

public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.MENU, menu);

    // change color for icon 0 
    Drawable yourdrawable = menu.getItem(0).getIcon(); // change 0 with 1,2 ... 
    yourdrawable.mutate();
    yourdrawable.setColorFilter(getResources().getColor(R.color.white), PorterDuff.Mode.SRC_IN);
    return true;
}       

我尝试了被接受的答案,但对我来说没有起作用。这个答案是我按时完成作业的救命稻草。 - undefined

7

菜单中图标的颜色可以在Layout/activity_main.xml中更改

将此行 app:itemIconTint="@color/red_warning" 添加到 com.google.android.material.navigation.NavigationView 标签中。

    <?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
        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:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

    <include
            layout="@layout/app_bar_main"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>


    <com.google.android.material.navigation.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer"
            app:itemIconTint="@color/red_warning"
    />


</androidx.drawerlayout.widget.DrawerLayout>

enter image description here


这太完美了,它起作用了! app:itemTextColor="@color/white" app:subheaderColor="@color/white" - Rafiqullah Taibzada

6

简短回答 --> 使用 app:iconTint="?android:textColorPrimary" 如果你想要图标颜色为白色,写入: android:theme="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar" 如果你想要黑色颜色,写入:android:theme="@style/ThemeOverlay.MaterialComponents.Light" 到你的工具栏中。


你好,欢迎来到StackOverflow。由于这个问题已经有几个受欢迎的答案,包括一个被接受的答案,请编辑你的回答,描述这个稍微不同的解决方案的行为以及你认为它有何帮助。谢谢。 - MandyShaw
<style name="AppTheme" parent="Theme.MaterialComponents.Light">, its works for me thanks - Ashwin H

3
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
    menuInflater.inflate(R.menu.menu_confirm, menu);
    MenuItem action_done = menu.findItem(R.id.action_done);
    action_done.setIcon(R.drawable.ic_filter);
    Utils.menuIconColor(action_done, Color.WHITE);
    super.onCreateOptionsMenu(menu, menuInflater);
}

public static void menuIconColor(MenuItem menuItem, int color) {
    Drawable drawable = menuItem.getIcon();
    if (drawable != null) {
        drawable.mutate();
        drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
    }
}

2
 app:iconTint="@color/colorWhite" 

请在导航视图属性中添加此内容

例如 -

<com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/main_color"
        app:itemBackground="@drawable/divider_menu_items"
        app:itemTextColor="@color/colorWhite"
        app:itemIconTint="@color/colorWhite"
        app:menu="@menu/activity_main_drawer"/>

将所有菜单项图标转换为您指定的颜色。

0

这种行为是预期的,因为ShareActionProvider负责创建视图以启用数据共享,并在托管项放置在溢出菜单上时显示带有共享活动的子菜单。

根据文档

这意味着在使用它时,您无法控制视图的自定义。


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