我的主要活动中有此功能,但不显示徽章:
private fun setFindShiftBadge(state: HomeState) {
val findShiftsBadge = BadgeDrawable.create(this)
home_framelayout.foreground = findShiftsBadge
findShiftsBadge.badgeGravity = BadgeDrawable.TOP_END
findShiftsBadge.backgroundColor = resources.getColor(R.color.colorWhite)
findShiftsBadge.badgeTextColor = resources.getColor(R.color.colorPrimary)
findShiftsBadge.number = state.availableShifts.size
}
Run Code Online (Sandbox Code Playgroud)
在同一个活动中,我有这个显示徽章的功能:
private fun setMyPostedShiftBadge(state: HomeState) {
val userShiftsBadge: BadgeDrawable =
bottom_navigation_view.getOrCreateBadge(R.id.bottom_navigation_my_posted_shifts_text)
userShiftsBadge.number = state.userShifts.size
userShiftsBadge.backgroundColor = resources.getColor(R.color.colorPrimary)
}
Run Code Online (Sandbox Code Playgroud)
现在我明白第二个函数可以工作,因为徽章设置在BottomNavigationView. 具有讽刺意味的是,BottomNavigationView扩展FrameLayout。在 Android 文档中:
使用 attachBadgeDrawable(BadgeDrawable, View, FrameLayout) 将 BadgeDrawable 作为 ViewOverlay 添加到所需的锚视图。使用 updateBadgeCoordinates(View, ViewGroup) 根据锚视图更新 BadgeDrawable BadgeDrawable 的坐标(中心和边界)。
他们说要使用这段代码,For API 18+ (APIs supported by ViewOverlay)我在第一个不起作用的函数中使用了它:
BadgeDrawable badgeDrawable …Run Code Online (Sandbox Code Playgroud) android badge android-view android-drawable android-framelayout