How to set icon color of MenuItem?

后端 未结 9 696
面向向阳花
面向向阳花 2021-01-30 12:37

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



        
相关标签:
9条回答
  • 2021-01-30 13:02

    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;
    }       
    
    0 讨论(0)
  • 2021-01-30 13:05

    Short and Sweet Answer--> app:iconTint="@color/yourcolor

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

    <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"/>
    
    0 讨论(0)
  • 2021-01-30 13:05

    This behaviour is expected, as the ShareActionProvider is

    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.

    0 讨论(0)
  • 2021-01-30 13:07
    @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);
        }
    }
    
    0 讨论(0)
  • 2021-01-30 13:08

    color of icons in menu can change in Layout/activity_main.xml

    set this line app:itemIconTint="@color/red_warning inside this tag 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>
    

    0 讨论(0)
  • 2021-01-30 13:10

    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:

    <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;
    }
    
    0 讨论(0)
提交回复
热议问题