【问题标题】:Disable ShiftNode of BottomNavigationView in support library 28.0.0在支持库 28.0.0 中禁用 BottomNavigationView 的 ShiftNode
【发布时间】:2019-01-09 15:26:30
【问题描述】:

将支持库升级到v-28.0.0 BottomNavigationView 后,移位模式未禁用。即使我正在使用app:labelVisibilityMode="labeled",但项目正在移动并且项目文本在选择时被剪切。

以前我使用反射来做到这一点。有没有其他方法可以禁用移位模式。

【问题讨论】:

  • BottomNavigationView 中有多少个菜单项?
  • @ChintanSoni 5 个菜单项。

标签: android user-interface bottomnavigationview


【解决方案1】:

对我来说,在 XML 中启用使用的移位模式,使用 match_parent 宽度

            app:labelVisibilityMode="selected"

禁用换档模式

            app:labelVisibilityMode="labeled"

& 删除你的思考。我的工作照常。

【讨论】:

  • 除了我希望标签出现在有空间的平板电脑上,而不是出现在没有空间的手机上。否则效果很好。
【解决方案2】:

也许这会有所帮助(它是 kotlin):

@SuppressLint("RestrictedApi")
fun disableShiftMode(view: BottomNavigationView) {
    val menuView = view.getChildAt(0) as BottomNavigationMenuView
    try {
        val shiftingMode = menuView.javaClass.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")
    } catch (e: IllegalAccessException) {
        Log.e(TAG, "Unable to change value of shift mode");
    }
}

【讨论】:

  • 此方法仅适用于28.0.0以下的支持库
  • 你可以尝试使用@SuppressLint("RestrictedApi") fun removeShiftMode(view: BottomNavigationView) { val menuView = view.getChildAt(0) as BottomNavigationMenuView menuView.labelVisibilityMode = LabelVisibilityMode.LABEL_VISIBILITY_LABELED menuView.buildMenuView() }
【解决方案3】:

尝试将app:itemTextAppearanceActiveapp:itemTextAppearanceInactive 设置为相同的textAppearance 样式,或者具有相同textSizefontFamily 的样式

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    相关资源
    最近更新 更多