动态更改导航视图项目颜色Android

vig*_*ion 15 android drawerlayout navigationview androiddesignsupport

我想建立一个导航抽屉,其中每个项目都有不同的选择颜色(图标色调和文本颜色),因为谷歌游戏商店有:

在此输入图像描述

我不确定他们是如何解决的,我认为他们使用不同的抽屉活动.我想使用片段,我想更改图标色调和文本颜色.我有什么想法可以做到这一点?我正在使用谷歌的设计支持库和带有导航视图的抽屉布局.

DJa*_*ari 36

使用app:itemIconTint你的NavigationView图标的,并使用app:itemTextColor了textColors

样品:

drawable/navigation_text_color :

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- This is used when the Navigation Item is checked -->
    <item android:color="#009688" android:state_checked="true" />
    <!-- This is the default text color -->
    <item android:color="#E91E63" />
</selector>
Run Code Online (Sandbox Code Playgroud)

并且layout:

<android.support.design.widget.NavigationView
       .
       .
       app:itemTextColor="@drawable/navigation_text_color"/>
Run Code Online (Sandbox Code Playgroud)


Rob*_*ert 5

如果动态地用编程方式表示你可以试试这个:

// 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);
Run Code Online (Sandbox Code Playgroud)

因此,您可以为白天或夜晚等不同设置定义多种颜色.