如何为导航抽屉的菜单项赋予颜色? [英] How to give color to menu items for Navigation drawer?
问题描述
我正在创建导航抽屉,我看到playtore带有彩色菜单图标 我想知道我该怎么做. 我试图通过colorFilter在菜单图标上应用颜色,但应用程序强制关闭
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
这是我的代码
<?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>
推荐答案
-
要将所有图标刷成特定颜色,您需要在
NavigationView
中添加app:itemIconTint
:
To brush all icons into particular color, you need to add
app:itemIconTint
into yourNavigationView
:
<android.support.design.widget.NavigationView
........
app:itemIconTint="<your color>"/>
仅用两种颜色刷图标 :
创建选择器:
<?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>
并将其作为app:itemIconTint
应用于您的NavigationView
:
And apply it as a app:itemIconTint
in your NavigationView
:
<android.support.design.widget.NavigationView
........
app:itemIconTint="@drawable/tint_color_selector"/>
最后一步-将android:checked="true"
添加到抽屉式菜单xml中的MenuItems
中,您想要进行不同的刷涂:
Last step - to add android:checked="true"
to the MenuItems
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" />
要用不同的颜色刷所有图标,就像Google在Google Play中使用的那样:
To brush all icons in different colors, like Google has in Google Play:
禁用图标的着色:
navigationView.setItemIconTintList(null);
将所需的图标添加到res/drawable
,并在菜单的xml中将其指定为android:icon
. (我可以为android图标 icons8.com/web-app/new-icons推荐一项不错的服务/android )
Add icons you want to the res/drawable
and specify them as android:icon
in your menu's xml. (I can recommend a nice service for android icons icons8.com/web-app/new-icons/android)
您可以绘制现有的图标,而不是上传新的彩色图标,但这很hacky:
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);
当心!!在模板项目的res/drawables-v21
中,Google使用VectorDrawable
而不是旧的BitmapDrawables
,因此该代码在那里不起作用.
Watch out! In res/drawables-v21
in template project, Google uses VectorDrawable
s instead of old BitmapDrawables
, so this code won't work there.
我希望这会有所帮助.
这篇关于如何为导航抽屉的菜单项赋予颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!