Change Navigation View Item Color Dynamically Android

前端 未结 5 2154
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-03 02:04

I\'d like to build a navigation drawer where each item has a different selection color (the icon tint and text color) as the google play store has:

5条回答
  •  猫巷女王i
    2020-12-03 02:13

    If by dynamically you mean programmatically you could try this:

    // FOR NAVIGATION VIEW ITEM TEXT COLOR
    int[][] states = new int[][]{
            new int[]{-android.R.attr.state_checked},  // unchecked
            new int[]{android.R.attr.state_checked},   // checked
            new int[]{}                                // default
    };
    
    // Fill in color corresponding to state defined in state
    int[] colors = new int[]{
            Color.parseColor("#747474"),
            Color.parseColor("#007f42"),
            Color.parseColor("#747474"),
    };
    
    ColorStateList navigationViewColorStateList = new ColorStateList(states, colors);
    
    // apply to text color
    navigationView.setItemTextColor(navigationViewColorStateList);
    
    // apply to icon color
    navigationView.setItemIconTintList(navigationViewColorStateList);
    

    So you could define multiple colors for different settings like Day or Night.

提交回复
热议问题