BadgeDrawable是否不适用于FrameLayout中的视图,例如按钮,图像,文本视图等? [英] Does BadgeDrawable not work for views within a FrameLayout such as buttons, images, textviews etc.?

查看:355
本文介绍了BadgeDrawable是否不适用于FrameLayout中的视图,例如按钮,图像,文本视图等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have this function in my main activity which doesn't display a badge:

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
 }

在同一活动中,我具有显示徽章的功能:

In the same activity, I have this function which displays a badge:

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文档中: 使用attachBadgeDrawable(BadgeDrawable ,视图,FrameLayout). 使用updateBadgeCoordinates(View,ViewGroup)根据其锚点视图更新BadgeDrawable BadgeDrawable的坐标(中心和边界).

Now I understand that the second function works because the badge is set within a BottomNavigationView. Ironically, BottomNavigationView extends FrameLayout. In the Android Documentation: 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),我在第一个无效的函数中使用了该代码:

They say to use this code, For API 18+ (APIs supported by ViewOverlay) which I use in the first function that doesn't work:

 BadgeDrawable badgeDrawable = BadgeDrawable.create(context);
 BadgeUtils.attachBadgeDrawable(badgeDrawable, anchor, null);

我也尝试过解决方案,但是它也没有用.我知道有一些解决方法,例如:

I have also tried this solution but it didn't work either. I'm aware there are workarounds such as:

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

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

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

This seems dirty to me since I know that there is a better way to do this. I just can't seem to figure it out. What am I missing? Is this even possible? How have you been able to do this without having to write a workaround? Thanks for your time! Looking forward to learning from your questions and answers!! Have a fantastic day!

推荐答案

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

BottomNavigationView.getOrCreateBadge() does not only assign the BadgeDrawable as foreground Drawable, it also sets the drawable bounds. Without this step, they stay at (0,0,0,0), so there is nothing to draw.

为了设置界限,我们为BadgeDrawable

In order to set the bounds, let's introduce an extension function for BadgeDrawable

/**
 * Inspired by BadgeUtils in com.google.android.material library
 *
 * Sets the bounds of a BadgeDrawable 
 */
private fun BadgeDrawable.setBoundsFor(@NonNull anchor: View, @NonNull parent: FrameLayout){
    val rect = Rect()
    parent.getDrawingRect(rect)
    this.setBounds(rect)
    this.updateBadgeCoordinates(anchor, parent)
}

将此功能与BadgeDrawable一起用于FrameLayout:

private fun setFindShiftBadge(state: HomeState) {
    val findShiftsBadge = BadgeDrawable.create(this)
    // configure the badge drawable
    findShiftsBadge.badgeGravity = BadgeDrawable.TOP_END
    findShiftsBadge.backgroundColor = resources.getColor(R.color.colorWhite)
    findShiftsBadge.badgeTextColor = resources.getColor(R.color.colorPrimary)
    findShiftsBadge.number = state.availableShifts.size
    // set bounds inside which it will be drawn
    findShiftsBadge.setBoundsFor(btn_badge, home_framelayout)
    // assign as foreground drawable
    home_framelayout.foreground = findShiftsBadge
}

这篇关于BadgeDrawable是否不适用于FrameLayout中的视图,例如按钮,图像,文本视图等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆