Changing text color of menu item in navigation drawer

后端 未结 18 1195

I\'m trying to add a night theme for my app and I\'ve wasted nearly three hours just trying to make the text and icons in my navigation drawer turn white along with the dark

18条回答
  •  悲哀的现实
    2020-11-28 04:45

    In my case, I needed to change the color of just one menu item - "Logout". I had to run a recursion and changed the title color:

    NavigationView nvDrawer;
    Menu menu = nvDrawer.getMenu();
        for (int i = 0; i < menu.size(); i ++){
            MenuItem menuItem = menu.getItem(i);
    
            if (menuItem.getTitle().equals("Logout")){
    
                SpannableString spanString = new SpannableString(menuItem.getTitle().toString());
                spanString.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.red)), 0, spanString.length(), 0);
                menuItem.setTitle(spanString);
    
            }
    
        }
    

    I did this in the Activity's onCreate method.

提交回复
热议问题