更改检查菜单项的颜色在导航抽屉 [英] Change the color of a checked menu item in a navigation drawer

查看:286
本文介绍了更改检查菜单项的颜色在导航抽屉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是最新的Andr​​oid设计支持库来实现导航抽屉里我的应用程序。

I am using the new Android Design Support library to implement a navigation drawer in my application.

我无法弄清楚如何改变所选项目的颜色!

这是菜单的XML:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
    <item
        android:id="@+id/navigation_item_1"
        android:icon="@drawable/ic_1"
        android:title="@string/navigation_item_1"/>

    <item
        android:id="@+id/navigation_item_2"
        android:icon="@drawable/ic_2"
        android:title="@string/navigation_item_2"/>
</group>

这里是navigationview XML被放置在一个 android.support.v4.widget.DrawerLayout

And here is the navigationview xml which is placed inside a android.support.v4.widget.DrawerLayout :

<android.support.design.widget.NavigationView
    android:id="@+id/activity_main_navigationview"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_header"
    app:itemIconTint="@color/black"
    app:itemTextColor="@color/primary_text"
    app:menu="@menu/menu_drawer">

    <TextView
        android:id="@+id/main_activity_version"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:textColor="@color/primary_text" />

</android.support.design.widget.NavigationView>

感谢您的帮助!

我已经看了解决方案,比如这个:更改安卓菜单。

I have already looked at solutions such as this one : Change background color of android menu.

这似乎是一个相当的黑客,我认为,在新的设计支持库,一些清洁剂会相继出台?

It seems to be quite a hack and I thought that with the new Design Support Library, something cleaner would have been introduced?

推荐答案

那么你可以做到这一点使用的颜色状态资源。如果您发现在你的 NavigationView 您使用的是

Well you can achieve this using Color State Resource. If you notice inside your NavigationView you're using

 app:itemIconTint="@color/black"
 app:itemTextColor="@color/primary_text"

下面,而不是使用 @彩色/黑白 @色/ primary_test ,使用颜色状态列表资源。为此,首先创建一个新的 XML (如drawer_item.xml)在颜色目录(这应该是在 RES 目录)。如果你没有一个名为颜色已,创建一个。

Here instead of using @color/black or @color/primary_test , use a Color State List Resource. For that , first create a new xml (e.g drawer_item.xml) inside color directory (which should be inside res directory.) If you don't have a directory named color already , create one.

现在里面 drawer_item.xml 做这样的事

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="checked state color" android:state_checked= "true" />
<item android:color="your default color" />

最后一步是改变你的 NavigationView

<android.support.design.widget.NavigationView
android:id="@+id/activity_main_navigationview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/drawer_header"
app:itemIconTint="@color/drawer_item" //notice here
app:itemTextColor="@color/drawer_item" //and here
app:itemBackground="@color/drawer_item"//and here for changing the background color of the item which is checked
app:menu="@menu/menu_drawer">

这样你可以使用不同的颜色状态列表资源 IconTint,ItemTextColor,ItemBackground

Like this you can use separate Color State List Resources for IconTint,ItemTextColor,ItemBackground.

现在,当您设置一个项目为检查(无论是在XML或以编程方式),具体项目将有不同的颜色,未选中的。

Now when you set an item as checked (either in xml or programmatically) , the particular item will have different color than the unchecked ones.

这篇关于更改检查菜单项的颜色在导航抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆