【问题标题】:BottomNavigationView - why is there not enough space for the badge Drawable?BottomNavigationView - 为什么没有足够的空间放置徽章 Drawable?
【发布时间】:2021-06-17 12:38:37
【问题描述】:

我使用材料库中的BottomNavigationViewconst val material = "com.google.android.material:material:1.4.0-rc01"

并使用以下绑定设置/更新其上的徽章:

@BindingAdapter("cartCount")
fun BottomNavigationView.updateCartCount(count: Int) {
    getOrCreateBadge(R.id.action_cart).apply {
        isVisible = count > 0
        if (isVisible) number = count
    }
}

这是 XML 中的视图(在 ConstraintLayout 内)

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav"
        cartCount="@{viewModel.cartCount}"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:elevation="8dp"
        app:itemIconTint="@color/bottom_nav_item_color"
        app:itemTextColor="@color/bottom_nav_item_color"
        app:labelVisibilityMode="labeled"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/menu_bottom_nav" />

我的问题是徽章似乎没有足够的空间:

选择项目时更明显:

我试图解决这个问题,但无济于事:

  • 使用不同的材料版本,尝试不同的版本(从 1.3.0 开始)
  • 增加BottomNavigationView 的高度,这里我将其设置为荒谬的100dp:

  • 更改图标的大小
  • 设置bottom_navigation_notification_padding 和来自this answer 的一些其他属性
  • 在材料release notes中寻找一些信息

我找不到任何与我的问题相关的“已知问题”。我错过了什么?

【问题讨论】:

    标签: android bottomnavigationview material-components-android


    【解决方案1】:

    在 com.google.android.material:material v1.3.0 及更高版本中,您可以使用BadgeDrawable 中的setVerticalOffsetsetHorizontalOffset 以特定像素量垂直或水平移动徽章。

    setVerticalOffset(int px)

    设置将此徽章垂直移动多少(以像素为单位) 其锚点的中心。

    setHorizontalOffset(int px)

    设置将此徽章水平移动多少(以像素为单位) 其锚点的中心。

    例子:

    val menuItemId: Int = bottomNavigation.getMenu().getItem(1).getItemId()
    val badge: BadgeDrawable = bottomNavigation.getOrCreateBadge(menuItemId)
    badge.setVisible(true)
    badge.setNumber(4)
    badge.setVerticalOffset(10) //value in pix
    badge.setHorizontalOffset(-10) //value in pix
    

    带和不带偏移的结果:

    【讨论】:

    • 我不知道这个 API,谢谢你的提示!应用一个小的偏移量确实解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 2022-09-28
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 2019-01-21
    • 2012-04-22
    • 1970-01-01
    相关资源
    最近更新 更多