简体   繁体   English

单击前更改导航抽屉项目的颜色

[英]Changing color of navigation drawer item before clicking it

I am changing the color of selected item of the navigation drawer and that is working fine through the following code 我正在更改导航抽屉中所选项目的颜色,并且通过以下代码可以正常工作

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:itemTextAppearance="@style/NavDrawerTextStyle"
        app:menu="@menu/activity_main_drawer"
        app:itemTextColor="@color/nav_item_color"
        android:background="#000000"/>

</android.support.v4.widget.DrawerLayout> 

following is the code line doing the trick for me 以下是为我做花招的代码行

app:itemTextColor="@color/nav_item_color"

code of xml from color directory 颜色目录中的xml代码

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#81DDFF" android:state_checked="true" style="@style/NavDrawerTextStyle"/>
    <item android:color="#FFFFFF" style="@style/NavDrawerTextStyle"/>
</selector>

Now the problem for me is I want to change the color of first item (before it is clicked as the screen corresponding to first item is there) of Navigation drawer when the drawer opens for the first time. 现在,对我来说,问题是,当抽屉第一次打开时,我想更改导航抽屉的第一项的颜色(在单击之前,因为存在与第一项相对应的屏幕)。

In other words how can I change the color of a single navigation drawer item from my activity class? 换句话说,如何从活动类中更改单个导航抽屉项目的颜色? Thank you 谢谢

Here is the screenshot when drawer opens for the first time and I wanted the color of login item light blue 这是抽屉第一次打开时的屏幕截图,我希望登录项的颜色为浅蓝色

When login item is clicked and drawer opens again 单击登录项并再次打开抽屉时

在此处输入图片说明

you can set selected manually 您可以手动选择

navigationView.getMenu().getItem(0).setChecked(true);

or you can use 或者你可以使用

navigationView.setCheckedItem(menuItemid);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM