How to disable BottomNavigationView shift mode?

后端 未结 21 1987
Happy的楠姐
Happy的楠姐 2020-11-22 15:55

BottomNavigationView doesn\'t show menu\'s title that are inactive.

How to show titles of all menu elements in bottomNavigationBar? The problem is that in my case s

21条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 16:46

    Przemysław's answer in Kotlin as an extension function

    @SuppressLint("RestrictedApi")
    fun BottomNavigationView.disableShiftMode() {
        val menuView = getChildAt(0) as BottomNavigationMenuView
        try {
            val shiftingMode = menuView::class.java.getDeclaredField("mShiftingMode")
            shiftingMode.isAccessible = true
            shiftingMode.setBoolean(menuView, false)
            shiftingMode.isAccessible = false
            for (i in 0 until menuView.childCount) {
                val item = menuView.getChildAt(i) as BottomNavigationItemView
                item.setShiftingMode(false)
                // set once again checked value, so view will be updated
                item.setChecked(item.itemData.isChecked)
            }
        } catch (e: NoSuchFieldException) {
            Log.e(TAG, "Unable to get shift mode field", e)
        } catch (e: IllegalStateException) {
            Log.e(TAG, "Unable to change value of shift mode", e)
        }
    }
    

    Usage (with Kotlin Android Extensions):

    bottom_navigation_view.disableShiftMode()
    

提交回复
热议问题