BadgeDrawable是否不适用于FrameLayout中的视图,例如按钮,图像,文本视图等?

问题描述 投票:1回答:1

我在不显示徽章的主要活动中具有此功能:

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,但它也不起作用。我知道有一些解决方法,例如:

  1. 创建一个可绘制对象,然后将其设置到所需的视图上
  2. 将TextView放置在该绘图中,然后使用所需的编号进行更新
  3. 在有任何数字时切换可绘制对象的可见性。

我在implementation 'com.google.android.material:material:1.2.0-alpha04'上>

这对我来说似乎很肮脏,因为我知道有更好的方法可以做到这一点。我似乎无法弄清楚。我想念什么?这有可能吗?您如何能够做到这一点而无需编写替代方法?谢谢你的时间!期待从您的问题和答案中学习!!祝你有美好的一天!

我在不显示徽章的主要活动中具有此功能:私人乐趣setFindShiftBadge(state:HomeState){val findShiftsBadge = BadgeDrawable.create(this)...

android android-view android-drawable android-framelayout badge
1个回答
0
投票

BottomNavigationView.getOrCreateBadge()不仅将BadgeDrawable分配为前景Drawable,而且还设置了可绘制范围。没有此步骤,它们将停留在(0,0,0,0),因此没有可绘制的内容。

© www.soinside.com 2019 - 2024. All rights reserved.