【问题标题】:How to add a number to Bottom Bar like the image [duplicate]如何像图像一样向底栏添加数字[重复]
【发布时间】:2020-03-20 06:40:51
【问题描述】:

我目前使用的是平滑底栏库。

【问题讨论】:

  • 你的平滑底栏库是否支持徽章。

标签: android bottombar


【解决方案1】:

您想在BottomNavigationView 菜单中添加徽章计数吗?

然后使用MaterialComponents

implementation 'com.google.android.material:material:1.2.0-alpha03'

使用materialcomponent主题style.xml

<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
</style>

在你的布局文件中

<com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_nav"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:menu="@menu/bottom_nav_menu"/>

添加徽章计数,如

val bottonnavBar = findViewById<BottomNavigationView>(R.id.bottom_nav)
var badge = bottonnavBar.getOrCreateBadge(R.id.action_add) //R.id.action_add is menu id
badge.number = 2 // it will show count on icon

希望对你有帮助...

【讨论】:

  • 如果有效,请将其标记为已接受...
【解决方案2】:

你可以使用材料设计指南来做到这一点from here。另外,不要忘记为android添加材料设计库guide here.

【讨论】:

    【解决方案3】:

    如果您使用的是 BottomNavigationView,那么这就像小菜一碟一样简单。 BottomNavigationView 提供 BadgeDrawable API,您可以使用它来显示动态信息,例如待处理的请求数。

    根据谷歌文档

    默认情况下,BadgeDrawable 与其锚点视图的顶部和结束边缘对齐(有一些偏移)。调用 #setBadgeGravity(int) 将其更改为其他支持的模式之一。

    注意:这仍在开发中,可能不支持 Android 组件通常支持的全部自定义 Material(例如主题属性)。

    您可以在此处查看 BadgeDrawable API 文档的详细信息 https://developer.android.com/reference/com/google/android/material/badge/BadgeDrawable

    【讨论】:

      猜你喜欢
      • 2021-07-26
      • 2020-04-11
      • 1970-01-01
      • 2013-04-12
      • 2013-03-25
      • 2016-04-26
      • 1970-01-01
      • 2011-02-04
      • 2014-06-22
      相关资源
      最近更新 更多