java - android,NavigationView的setItemTextColor和setItemIconTinList没有效果

标签 java android android-navigationview

对不起我的英语。

我需要动态设置 NavigationView 的项目文本和项目图标颜色,但由于某些原因这不起作用。

这将是一个错误还是我做错了什么? XML 运行良好,但是当我执行以下操作时,不是:

我的代码:

navigationView = (NavigationView) findViewById(R.id.nav_view);

        int[][] states = new int[][] {
                new int[] {  }, // default
                new int[] { android.R.attr.state_focused, android.R.attr.state_pressed },  // pressed
                new int[] { android.R.attr.state_selected } // selected
        };

        int[] colors = new int[] {
                colorDefault,
                colorFocused,
                colorSelected
        };

        ColorStateList myList = new ColorStateList(states, colors);
        navigationView.setItemTextColor(myList);
        navigationView.setItemIconTintList(myList);

出于某种原因,我只得到第一种颜色:(

最佳答案

int[][] states = new int[][] {
            new int[] { android.R.attr.state_selected } // selected               
            new int[] {  } // default
    };

int[] colors = new int[] {
        colorSelected,            
        colorDefault
};
ColorStateList colorList = new ColorStateList(states, colors);
navigationView.setItemTextColor(colorList);

仅使用选中的和默认的导航文本状态。正如马泰所说。您需要按最具体情况的顺序使用。

关于java - android,NavigationView的setItemTextColor和setItemIconTinList没有效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33114154/

相关文章:

java - 运行Spring MVC项目时出错

java - 如何在 google colab 上运行 stanford corenlp 服务器?

android - JDK 7 和安卓 IDE

android - OnClick 更改表格行背景颜色

android - 在抽屉导航菜单中以编程方式添加项目

java - 英雄联盟通过其RESTful API阅读 block /关键帧

java - 在堆栈的 ArrayList 中,如果堆栈为空,为什么索引不正确?

java - 在 libgdx 中裁剪相机 View

android - 使用图像作为导航菜单抽屉菜单中的项目

android - Material 设计中的底部抽屉