NavigationView(更改所选项目的颜色) [英] NavigationView(Change Color of Selected Item)

查看:77
本文介绍了NavigationView(更改所选项目的颜色)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过编程方式更改所选商品的商品颜色
在导航抽屉中?
我可以使用app:itemTextColor,但是问题是如果我使用过此代码,
菜单项中选中的内容将不会突出显示.

Is there any way to change the Item color programmatically of the selected item
in the navigation drawer?
I'am able to use the app:itemTextColor, but the problem is if i used this,
the checked in the menu item will not highlight.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/ic_home_black_24dp"
            android:title="Home" />
        <item
            android:id="@+id/nav_search"
            android:icon="@drawable/ic_search_black"
            android:title="Search Location" />
        <item
            android:id="@+id/nav_fav"
            android:icon="@drawable/ic_favorite"
            android:title="Favorites" />
        <item
            android:id="@+id/nav_route"
            android:icon="@drawable/ic_place"
            android:title="Route" />

        <item
            android:id="@+id/nav_recent"
            android:icon="@drawable/ic_nav_route"
            android:title="Recent Location" />
    </group>

    <item android:title="Others">
        <menu>
            <item
                android:id="@+id/nav_settings"
                android:checked="true"
                android:checkable="true"
                android:icon="@drawable/ic_settings"
                android:title="Settings" />
            <item
                android:id="@+id/nav_about"
                android:icon="@android:drawable/ic_menu_send"
                android:title="About" />
        </menu>
    </item>
</menu>

推荐答案

创建选择器

<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/primary" android:state_checked="true" />
        <item android:drawable="@android:color/transparent" />
</selector>

并设置

app:itemBackground="@drawable/nav_view_item_background"

然后选中的项目将突出显示.

then the selected item will be highlighted.

如果您想更改文本颜色,请设置

if you want to change the text color then set

app:itemTextColor="@drawable/nav_view_item_textcolor"

并为其创建一个选择器

<selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:color="@android:color/white" android:state_checked="true" />
     <item android:color="@color/primary" />
</selector>

这篇关于NavigationView(更改所选项目的颜色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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