问题
I was creating Navigation Drawer and i saw playtore have colored menu icons i want to know how can i do this. I tried to apply colors by colorFilter on menu icons but app force closes
This is my code
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:id="@+id/grp1"
android:checkableBehavior="single">
<item
android:id="@+id/navigation_songs"
android:checked="true"
android:icon="@drawable/ic_audiotrack_white_24dp"
android:title="@string/songs" />
<item
android:id="@+id/navigation_albums"
android:icon="@drawable/ic_album_white_24dp"
android:title="@string/albums" />
<item
android:id="@+id/navigation_artist"
android:icon="@drawable/ic_person_white_24dp"
android:title="@string/artists" />
<item
android:id="@+id/navigation_playlist"
android:icon="@drawable/ic_playlist_play_white_24dp"
android:title="@string/playlist" />
</group>
<group
android:id="@+id/grp2"
android:checkableBehavior="none">
<item
android:id="@+id/navigation_about"
android:icon="@drawable/ic_info_white_24dp"
android:title="@string/about" />
<item
android:id="@+id/navigation_settings"
android:icon="@drawable/ic_settings_white_24dp"
android:title="@string/settings" />
</group>
</menu>
回答1:
To brush all icons into particular color, you need to add
app:itemIconTint
into yourNavigationView
:<android.support.design.widget.NavigationView ........ app:itemIconTint="<your color>"/>
To brush your icons in only 2 colors:
Create a Selector:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="#0000FF" android:state_checked= "true" /> <item android:color="#FF0000" /> </selector>
And apply it as a
app:itemIconTint
in yourNavigationView
:<android.support.design.widget.NavigationView ........ app:itemIconTint="@drawable/tint_color_selector"/>
Last step - to add
android:checked="true"
to theMenuItems
in your drawer's menu xml you want to brush different:<item android:id="@+id/nav_slideshow" android:checked="true" android:icon="@drawable/ic_menu_slideshow" android:title="Slideshow" />
To brush all icons in different colors, like Google has in Google Play:
Disable tinting of your icons:
navigationView.setItemIconTintList(null);
Add icons you want to the
res/drawable
and specify them asandroid:icon
in your menu's xml. (I can recommend a nice service for android icons icons8.com/web-app/new-icons/android)Instead of uploading new colorful icons, you can paint existing ones, but it's very hacky:
Drawable oldIcon = navigationView .getMenu() .findItem(R.id.nav_gallery) .getIcon(); if (!(oldIcon instanceof BitmapDrawable)) { return; } Bitmap immutable = ((BitmapDrawable)oldIcon).getBitmap(); Bitmap mutable = immutable.copy(Bitmap.Config.ARGB_8888, true); Canvas c = new Canvas(mutable); Paint p = new Paint(); p.setColor(Color.RED); p.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY)); c.drawBitmap(mutable, 0.f, 0.f, p); BitmapDrawable newIcon = new BitmapDrawable(getResources(), mutable); navigationView .getMenu() .findItem(R.id.nav_gallery) .setIcon(newIcon);
Watch out! In
res/drawables-v21
in template project, Google usesVectorDrawable
s instead of oldBitmapDrawables
, so this code won't work there.
I hope, it helps.
来源:https://stackoverflow.com/questions/34705823/how-to-give-color-to-menu-items-for-navigation-drawer