如何设置MenuItem的图标颜色?

时间:2022-11-20 22:42:59

I defined a menu item that has ShareActionProvider and share white icon like so :

我定义了一个具有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"/>

But when I launch the application, I get a different black share icon. How to set the share icon to be white?

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

Here is the result that I have 如何设置MenuItem的图标颜色?

这是我的结果

7 个解决方案

#1


33  

This is a theming issue. Depending on your current theme, you need to set the correct ActionBar overlay theme. The Action Provider reads a value in the theme (which indicates if the theme is dark or light) to determine the color of the icon.

这是一个主题问题。根据您当前的主题,您需要设置正确的ActionBar叠加主题。动作提供者读取主题中的值(指示主题是暗还是亮)以确定图标的颜色。

If your main theme is light and your ActionBar is dark, your ActionBar/Toolbar must use the theme ThemeOverlay.AppCompat.Dark.ActionBar.

如果您的主题很轻且ActionBar很暗,则ActionBar / Toolbar必须使用主题ThemeOverlay.AppCompat.Dark.ActionBar。

#2


53  

The icon is actually provided by the ShareActionProvider and you can't change it afaik. You can, however, customize the color by setting the textColorPrimary in your styles.xml:

该图标实际上是由ShareActionProvider提供的,您无法将其更改为afaik。但是,您可以通过在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>

For any custom icons, you would have to color them yourself, ie.

对于任何自定义图标,您必须自己着色,即。

@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;
}

#3


23  

try this :

尝试这个 :

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

#4


2  

add app:iconTint="@color/yourcolor" in your MenuItem for change the Icon color.

在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"/>

#5


0  

This behaviour is expected, as the ShareActionProvider is

正如ShareActionProvider一样,这种行为是预期的

responsible for creating views that enable data sharing and also to show a sub menu with sharing activities if the hosting item is placed on the overflow menu.

负责创建启用数据共享的视图,并且如果托管项目位于溢出菜单上,则还显示具有共享活动的子菜单。

according to the documentation.

根据文件。

This means that you don't have control over the customization of the view when using it.

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

#6


0  

@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);
    }
}

#7


-1  

I used this library to achieve this menu:

我使用这个库来实现这个菜单:

如何设置MenuItem的图标颜色?

Take a look to my Github repo: https://github.com/lalongooo/permutas-sep/blob/master/app/src/main/java/com/permutassep/ui/ActivityMain.java#L171

看看我的Github回购:https://github.com/lalongooo/permutas-sep/blob/master/app/src/main/java/com/permutassep/ui/ActivityMain.java#L171

#1


33  

This is a theming issue. Depending on your current theme, you need to set the correct ActionBar overlay theme. The Action Provider reads a value in the theme (which indicates if the theme is dark or light) to determine the color of the icon.

这是一个主题问题。根据您当前的主题,您需要设置正确的ActionBar叠加主题。动作提供者读取主题中的值(指示主题是暗还是亮)以确定图标的颜色。

If your main theme is light and your ActionBar is dark, your ActionBar/Toolbar must use the theme ThemeOverlay.AppCompat.Dark.ActionBar.

如果您的主题很轻且ActionBar很暗,则ActionBar / Toolbar必须使用主题ThemeOverlay.AppCompat.Dark.ActionBar。

#2


53  

The icon is actually provided by the ShareActionProvider and you can't change it afaik. You can, however, customize the color by setting the textColorPrimary in your styles.xml:

该图标实际上是由ShareActionProvider提供的,您无法将其更改为afaik。但是,您可以通过在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>

For any custom icons, you would have to color them yourself, ie.

对于任何自定义图标,您必须自己着色,即。

@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;
}

#3


23  

try this :

尝试这个 :

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

#4


2  

add app:iconTint="@color/yourcolor" in your MenuItem for change the Icon color.

在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"/>

#5


0  

This behaviour is expected, as the ShareActionProvider is

正如ShareActionProvider一样,这种行为是预期的

responsible for creating views that enable data sharing and also to show a sub menu with sharing activities if the hosting item is placed on the overflow menu.

负责创建启用数据共享的视图,并且如果托管项目位于溢出菜单上,则还显示具有共享活动的子菜单。

according to the documentation.

根据文件。

This means that you don't have control over the customization of the view when using it.

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

#6


0  

@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);
    }
}

#7


-1  

I used this library to achieve this menu:

我使用这个库来实现这个菜单:

如何设置MenuItem的图标颜色?

Take a look to my Github repo: https://github.com/lalongooo/permutas-sep/blob/master/app/src/main/java/com/permutassep/ui/ActivityMain.java#L171

看看我的Github回购:https://github.com/lalongooo/permutas-sep/blob/master/app/src/main/java/com/permutassep/ui/ActivityMain.java#L171