Badge count on Floating action button

前端 未结 8 1866
一整个雨季
一整个雨季 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:32

    One can simply use TextView by giving custom style for floating.

            <TextView
                    android:id="@+id/fabCounter"
                    style="@style/Widget.Design.FloatingActionButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_centerVertical="true"
                    android:layout_marginEnd="10dp"
                    android:padding="5dp"
                    android:text="10"
                    android:textColor="@android:color/black"
                    android:textSize="14sp" />
    

    0 讨论(0)
  • 2021-01-05 02:36

    try this changes:

    <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end|bottom"
            android:id="@+id/frameLayout">
    
            <android.support.design.widget.FloatingActionButton
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/listen_button"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@android:color/transparent"
                app:backgroundTint="#e3e3e3" />
    
           <TextView
                android:id="@+id/textOne"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="10"
                android:textColor="#FFF"
                android:textSize="10sp"
                android:textStyle="bold"
                android:background="@drawable/badge_circle"/>
    
        </FrameLayout>
    
    0 讨论(0)
  • 2021-01-05 02:44

    For this problem solution is android:elevation="7dp". But you are looking for something like this then use bellow code:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
         <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:layout_margin="@dimen/margin.2">
                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/fab_cart"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_alignParentRight="true"
                    android:layout_margin="@dimen/margin.2"
                    app:srcCompat="@drawable/ic_add_shopping_cart"
                    app:backgroundTint="@color/colorPrimaryDark" />
                <TextView
                    android:id="@+id/text_count"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:elevation="7dp"
                    android:gravity="center"
                    android:textColor="@color/white"
                    android:layout_alignParentRight="true"
                    android:textSize="@dimen/text.size.small"
                    android:background="@drawable/bg_fab"
                    tools:text="10" />
            </RelativeLayout>
    </RelativeLayout>
    
    0 讨论(0)
  • 2021-01-05 02:45

    I used the CounterFab library to achieve this.

    It is as simple as adding it to your XML layout file instead of the regular FloatingActionButton and calling counterFab.setCount(10); (See usage example here).

    0 讨论(0)
  • 2021-01-05 02:47

    As suggested by Daniel, you should not add badges to Floating Action Buttons. However, if you really have to do it, try adding this property to your TextView:

    android:elevation="7dp"
    
    0 讨论(0)
  • 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):

    <dimen name="mtrl_badge_with_text_radius">12dp</dimen>
    

    0 讨论(0)
提交回复
热议问题