Badge count on Floating action button

前端 未结 8 1928
一整个雨季
一整个雨季 2021-01-05 02:26

I would like to show count badge in front of Floating Action Button in android. I used FrameLayout in order to achieve that. My code is here



        
8条回答
  •  佛祖请我去吃肉
    2021-01-05 02:51

    You can use the BadgeDrawable provided by the Material Components Library.

    Something like:

        fab.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                BadgeDrawable badgeDrawable = BadgeDrawable.create(MainActivity.this);
                badgeDrawable.setNumber(2);
                //Important to change the position of the Badge
                badgeDrawable.setHorizontalOffset(30);
                badgeDrawable.setVerticalOffset(20);
    
                BadgeUtils.attachBadgeDrawable(badgeDrawable, fab, null);
    
                fab.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    

    Currently (1.3.0-alpha02) there isn't a method to change the radius, but you can override this dimen in your project (dimens.xml):

    12dp
    

提交回复
热议问题