Changing text color of menu item in navigation drawer

后端 未结 18 1198

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: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);
            }
    

提交回复
热议问题