Ely*_*nad 2 android kotlin bottomnavigationview
我正在寻找一种方法来以编程方式更改我的BottomNavigationView.
我找到了如何更改图标颜色,我按如下方式操作:
fun setMenu(selected: Int) {
bottomNavigationMenu?.let {
it.menu.findItem(R.id.bottom_menu_home).icon
.setColorFilter(getMenuColor(R.id.bottom_menu_home, selected), PorterDuff.Mode.SRC_ATOP)
// more items
it.menu.findItem(R.id.bottom_menu_fidelity)?.icon
?.setColorFilter(getMenuColor(R.id.bottom_menu_fidelity, selected), PorterDuff.Mode.SRC_ATOP)
}
}
fun getMenuColor(id: Int, selected: Int): Int {
if (Singletons.restaurant.title.isBlank())
getRestaurant()
else {
if (id == selected) return (Singletons.restaurant.color)
else return ContextCompat.getColor(this, R.color.grey7E)
}
return (0)
}
Run Code Online (Sandbox Code Playgroud)
现在我正在寻找如何更改标题颜色。我正在寻找类似的东西
it.menu.findItem(R.id.bottom_menu_home).title // etc
Run Code Online (Sandbox Code Playgroud)
但我找不到正确的函数来做到这一点。
这是我的main_bottom_menu.xml项目:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/bottom_menu_home"
android:enabled="true"
android:icon="@drawable/ic_home"
android:iconTint="@color/cardview_dark_background"
android:title="@string/bottom_menu_home"
android:visible="false"
app:showAsAction="ifRoom" />
<!-- 3 more items -->
<item
android:id="@+id/bottom_menu_profile"
android:enabled="true"
android:icon="@drawable/ic_profile"
android:iconTint="@color/cardview_dark_background"
android:title="@string/bottom_menu_profile"
android:visible="false"
app:showAsAction="ifRoom" />
</menu>
Run Code Online (Sandbox Code Playgroud)
奇怪的是,文本颜色等于 my colorPrimary,因此必须在某处设置它。目标是动态设置颜色(通过 API)。知道我的标题颜色设置在哪里吗?
要设置所有项目的颜色,您只需调用setItemTextColor()您的BottomNavigationView实例即可。
要单独设置它们,您可以使用SpannableString:
val titleSpannable = SpannableString("title")
titleSpannable.setSpan(
ForegroundColorSpan(Color.parseColor("#ffffff")),
0,
titleString.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
bottomNavigationView.menu.findItem(R.id.your_item_id).title = titleSpannable
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1313 次 |
| 最近记录: |