Changing text color of menu item in navigation drawer

后端 未结 18 1085

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:55

    add app:itemTextColor="#fff" in your NavigationView like

     <android.support.design.widget.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        app:menu="@menu/slider_menu"
        android:background="@color/colorAccent"
        app:itemTextColor="#fff"
        android:id="@+id/navigation_view"
        android:layout_gravity="start"/>
    
    0 讨论(0)
  • 2020-11-28 04:58

    NavigationView has a method called setItemTextColor(). It uses a ColorStateList.

    // FOR NAVIGATION VIEW ITEM TEXT COLOR
    int[][] state = new int[][] {
            new int[] {-android.R.attr.state_enabled}, // disabled
            new int[] {android.R.attr.state_enabled}, // enabled
            new int[] {-android.R.attr.state_checked}, // unchecked
            new int[] { android.R.attr.state_pressed}  // pressed
    
    };
    
    int[] color = new int[] {
            Color.WHITE,
            Color.WHITE,
            Color.WHITE,
            Color.WHITE
    };
    
    ColorStateList csl = new ColorStateList(state, color);
    
    
    // FOR NAVIGATION VIEW ITEM ICON COLOR
    int[][] states = new int[][] {
            new int[] {-android.R.attr.state_enabled}, // disabled
            new int[] {android.R.attr.state_enabled}, // enabled
            new int[] {-android.R.attr.state_checked}, // unchecked
            new int[] { android.R.attr.state_pressed}  // pressed
    
    };
    
    int[] colors = new int[] {
            Color.WHITE,
            Color.WHITE,
            Color.WHITE,
            Color.WHITE
    };
    
    ColorStateList csl2 = new ColorStateList(states, colors);
    

    Here is where I got that answer. And then right after assigning my NavigationView:

    if (nightMode == 0) {
                navigationView.setItemTextColor(csl);
                navigationView.setItemIconTintList(csl2);
            }
    
    0 讨论(0)
  • 2020-11-28 05:01

    You can use drawables in

    app:itemTextColor app:itemIconTint

    then you can control the checked state and normal state using a drawable

    <android.support.design.widget.NavigationView
                android:id="@+id/nav_view"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                app:itemHorizontalPadding="@dimen/margin_30"
                app:itemIconTint="@drawable/drawer_item_color"
                app:itemTextColor="@drawable/drawer_item_color"
                android:theme="@style/NavigationView"
                app:headerLayout="@layout/nav_header"
                app:menu="@menu/drawer_menu">
    

    drawer_item_color.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:color="@color/selectedColor" android:state_checked="true" />
        <item android:color="@color/normalColor" />
    </selector>
    
    0 讨论(0)
  • 2020-11-28 05:02

    I used below code to change Navigation drawer text color in my app.

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setItemTextColor(ColorStateList.valueOf(Color.WHITE));
    
    0 讨论(0)
  • 2020-11-28 05:05

    This works for me. in place of customTheme you can put you theme in styles. in this code you can also change the font and text size.

        <style name="MyTheme.NavMenu" parent="CustomTheme">
                <item name="android:textSize">16sp</item>
                <item name="android:fontFamily">@font/ssp_semi_bold</item>
                <item name="android:textColorPrimary">@color/yourcolor</item>
            </style>
    

    here is my navigation view

    <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true"
            app:theme="@style/MyTheme.NavMenu"
            app:headerLayout="@layout/nav_header_main"
            app:menu="@menu/activity_main_drawer">
    
            <include layout="@layout/layout_update_available"/>
    
        </android.support.design.widget.NavigationView>
    
    0 讨论(0)
  • 2020-11-28 05:05

    try this in java class

    yourNavigationView.setItemTextColor(new ColorStateList(
                new int [] [] {
                        new int [] {android.R.attr.state_pressed},
                        new int [] {android.R.attr.state_focused},
                        new int [] {}
                },
                new int [] {
                        Color.rgb (255, 128, 192),
                        Color.rgb (100, 200, 192),
                        Color.WHITE
                }
        ));
    
    0 讨论(0)
提交回复
热议问题