在 /res/color中创建一个新的 * . xml 文件(让我们把它命名为 State _ list. xml) ,其内容如下:
<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>
app:itemIconTint="@color/menu_text_color" //icon color
app:itemTextColor="@color/menu_text_color" //text color
app:itemBackground="@drawable/menu_background_color" //background
fun NavigationView.setTextColorForMenuItems(@ColorInt color: Int) {
for (i: Int in 0 until menu.size()) {
val menuItem = menu.getItem(i)
val spanString = SpannableString(menuItem.title.toString())
spanString.setSpan(ForegroundColorSpan(color), 0, spanString.length, 0)
menuItem.title = spanString
}
}