如何像图像一样将数字添加到底部栏

How to add a number to Bottom Bar like the image

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

您可以使用 material 设计指南来完成 from here. also, don't forget to add material design library for android guide here.

您想在 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

希望对您有所帮助...

如果您使用的是 BottomNavigationView,那么这很容易。 BottomNavigationView 提供了 BadgeDrawable API,您可以使用它来显示动态信息,例如一些待处理的请求。

根据 google 文档

By default, BadgeDrawable is aligned to the top and end edges of its anchor view (with some offsets). Call #setBadgeGravity(int) to change it to one of the other supported modes.

Note: This is still under development and may not support the full range of customization Material Android components generally support (e.g. themed attributes).

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