如何在Kotlin中设置单个菜单项的文本颜色?
private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
when (item.itemId) {
R.id.navigation_home -> {
toolbar.title = "Home"
item.actionview.SetColor(ContextCompat.getColor(applicationContext, R.color.highlight))
val homeFragment = HomeFragment.newInstance()
openFragment(homeFragment)
return@OnNavigationItemSelectedListener true
}
...
答案 0 :(得分:1)
您可以使用跨度字符串 使用以下功能:
fun getColoredSpannableString(s: String, color: Int): SpannableString {
val str = SpannableString(s)
str.setSpan(ForegroundColorSpan(color), 0, s.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
return str
}
fun makeMenuItemColored(mi: MenuItem, color: Int) {
mi.title = getColoredSpannableString(mi.title.toString(), color)
}
,并通过您的活动调用:
makeMenuItemColored(mi, ContextCompat.getColor(this, R.color.colorPrimary))
mi
是您的菜单项,然后将颜色更改为您喜欢的颜色。