android.support.design.widget.BottomNavigationView如何更改未选中项目的图标,而不是仅更改浅色

时间:2019-03-22 17:37:49

标签: android kotlin bottomnavigationview

首先,如果重复,请告诉我,因为我找不到它。

我正在使用带有自定义图标的 BottomNavigationView

我的问题是,在选择了一个项目之后,未选中该项目时,图标会更改其颜色/色调,而不是恢复为其初始状态。

仅在“消息”选项卡上出现问题。

这是初始状态 (语音气泡仅用于边框)

Initial stage

这是突出显示的地方

Highlighted stage

这是错误的 (语音气泡现在是纯白色,但应该只有边框)

Wrong sate

我尝试在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建议之后定义bottomNavBar选项卡的XML

bottomBar_menu

这是我添加的选择器

tab_selector

使用该解决方案,应用程序崩溃,尝试使用此日志为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

有人有另一个主意...我很高兴尝试任何事情,因为我没有主意

1 个答案:

答案 0 :(得分:0)

这就是我在this项目中所拥有的:

<?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
    }
}
相关问题