我在不显示徽章的主要活动中具有此功能:
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
}
在同一活动中,我具有显示徽章的功能:
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)
}
现在,我知道第二个功能可以工作,因为徽章设置在BottomNavigationView
中。具有讽刺意味的是,BottomNavigationView
扩展了FrameLayout
。在Android文档中:Add BadgeDrawable as a ViewOverlay to the desired anchor view using attachBadgeDrawable(BadgeDrawable, View, FrameLayout).
Update the BadgeDrawable BadgeDrawable's coordinates (center and bounds) based on its anchor view using updateBadgeCoordinates(View, ViewGroup).
他们说使用此代码,For API 18+ (APIs supported by ViewOverlay)
我在第一个不起作用的功能中使用:
BadgeDrawable badgeDrawable = BadgeDrawable.create(context);
BadgeUtils.attachBadgeDrawable(badgeDrawable, anchor, null);
我也尝试过此solution,但它也不起作用。我知道有一些解决方法,例如:
我在implementation 'com.google.android.material:material:1.2.0-alpha04'
上>
这对我来说似乎很肮脏,因为我知道有更好的方法可以做到这一点。我似乎无法弄清楚。我想念什么?这有可能吗?您如何能够做到这一点而无需编写替代方法?谢谢你的时间!期待从您的问题和答案中学习!!祝你有美好的一天!
我在不显示徽章的主要活动中具有此功能:私人乐趣setFindShiftBadge(state:HomeState){val findShiftsBadge = BadgeDrawable.create(this)...
BottomNavigationView.getOrCreateBadge()
不仅将BadgeDrawable
分配为前景Drawable
,而且还设置了可绘制范围。没有此步骤,它们将停留在(0,0,0,0),因此没有可绘制的内容。