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

问题描述 投票:0回答:1

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

我正在使用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

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

android kotlin bottomnavigationview
1个回答
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
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.