android - android.support.design.widget.BottomNavigationView 如何更改未选中项目的图标而不仅仅是色调
问题描述
首先,如果它重复,请告诉我,因为我找不到它。
我正在使用带有自定义图标的BottomNavigationView 。
我的问题是,在选择一个项目后,当它被取消选择时,图标会改变它的颜色/色调,而不是恢复到它的初始状态。
问题仅发生在消息选项卡上。
这是初始状态 (语音气泡仅是边框)
这是它突出显示的时候
这是错误的 (语音气泡现在是纯白色,但它应该只有一个边框)
我尝试在 OnNavigationItemSelectedListener 中访问该视图,但我无法...任何帮助都可以挽救生命;)
这是方法
private val onNavigationItemSelected = BottomNavigationView.OnNavigationItemSelectedListener {
var result = false
when (it.itemId) {
R.id.bottombarWalks -> {
it.icon = ResourcesCompat.getDrawable(resources, R.drawable.walks_on,null)
switchFragment(0, WalksFragment())
result = true
}
R.id.bottombarMembership -> {
it.icon = ResourcesCompat.getDrawable(resources, R.drawable.membership_on,null)
switchFragment(1, TabFragment.newInstance("Membership tab"))
result = true
}
R.id.bottombarHome -> {
it.icon = ResourcesCompat.getDrawable(resources, R.drawable.home_on,null)
switchFragment(2, HomeFragment())
result = true
}
R.id.bottombarMessages -> {
it.icon = ResourcesCompat.getDrawable(resources, R.drawable.messages_on,null)
switchFragment(3, MessagesTabFragment())
result = true
}
R.id.bottombarMore -> {
it.icon = ResourcesCompat.getDrawable(resources, R.drawable.more_on,null)
switchFragment(4, MoreFragment())
result = true
}
}
result
}
提前致谢
@FrancislainyCampos 解决方案
这是在应用 @FrancislainyCampos 建议后定义底部导航栏选项卡的 XML
这是我添加的选择器
使用该解决方案,应用程序在尝试使用以下日志填充 BottomNavBar 时崩溃:
java.lang.RuntimeException: Unable to start activity ComponentInfo{uk.org.ramblers.walkreg/uk.org.ramblers.walkreg.ui.MainActivity}: android.view.InflateException: Binary XML file line #22: Binary XML file line #22: Error inflating class android.support.design.widget.BottomNavigationView
[...]
Caused by: android.view.InflateException: Binary XML file line #22: Binary XML file line #22: Error inflating class android.support.design.widget.BottomNavigationView
Caused by: android.view.InflateException: Binary XML file line #22: Error inflating class android.support.design.widget.BottomNavigationView
Caused by: java.lang.reflect.InvocationTargetException
[...]
Caused by: android.content.res.Resources$NotFoundException: Drawable uk.org.ramblers.walkreg:drawable/tab_selector with resource ID #0x7f0700ce
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/tab_selector.xml from drawable resource ID #0x7f0700ce
有人有其他想法吗...我很乐意尝试任何事情,因为我没有想法
解决方案
这就是我在这个项目中所拥有的:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/orange" android:state_checked="true"/>
<item android:color="@color/white" android:state_checked="false"/>
</selector>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/tab_home"
android:icon="@drawable/home_blue"
android:title="@string/tab_home"
app:showAsAction="always"/>
<item
android:id="@+id/tab_location"
android:icon="@drawable/ic_location"
android:title="Location"
app:showAsAction="always"/>
<item
android:id="@+id/tab_tic_tac_toe"
android:icon="@drawable/ic_tic_tac_toe"
android:title="Tic Tac Toe"
app:showAsAction="always"/>
private fun bottomBarNavigationView() {
navigation.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
navigation.selectedItemId = R.id.tab_tic_tac_toe
}
private val onNavigationItemSelectedListener = object : OnNavigationItemSelectedListener {
override fun onNavigationItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.tab_home -> {
displayView(FRAG_COUNTER)
return true
}
R.id.tab_location -> {
displayView(FRAG_LOCATION)
return true
}
R.id.tab_tic_tac_toe -> {
displayView(FRAG_TIC_TAC)
return true
}
}
return false
}
}
推荐阅读
- aurelia - main.js Aurelia 中 setRoot 的依赖注入(用于 HttpFetch)
- c# - context include 有时会以无特定顺序返回列表项
- java - 无法弄清楚如何摆脱mysql上的语法错误
- visual-studio-code - 如何从 Visual Studio Code 的终端在 Visual Studio Code 的文本编辑器中打开文件?
- javascript - 在绘制对象期间添加延迟功能
- docker - 来自 nginx 的未知“服务器”变量
- windows - 在 Windows 环境变量中转义 @
- r - 如何根据条件从 R 中的大型数据集中删除一组特定数据?
- sql - 使用文本编辑器上的宏插入语句中的大型数据集
- sas - 命令变量出现在 proc freq 的绘图输出中