徽章指向浮动操作按钮

Fer*_*igi 6 android android-layout

我想在android中 Floating Action Button 前面显示计数徽章.我使用FrameLayout来实现这一目标.我的代码在这里

   <FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/send_button"
    android:layout_toLeftOf="@+id/send_button"
    android:layout_toStartOf="@+id/send_button"
    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"
        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"
        android:layout_gravity="right|bottom"
        android:layout_alignBottom="@+id/frameLayout"
        android:layout_toLeftOf="@+id/frameLayout"
        android:layout_toStartOf="@+id/frameLayout" />

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

我在FAB下方获得了计数徽章,如下所示

在此输入图像描述

我需要计数徽章在FAB前面.

Md *_*ury 8

对于这个问题的解决方案是android:elevation="7dp"。但是您正在寻找类似的东西,然后使用以下代码:

在此输入图像描述

<?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>
Run Code Online (Sandbox Code Playgroud)


Tom*_*mer 7

我使用CounterFab 库来实现此目的。

它就像将其添加到 XML 布局文件而不是常规的FloatingActionButton并调用一样简单(请参阅此处的counterFab.setCount(10);使用示例)。


Gab*_*tti 5

您可以使用BadgeDrawable材料组件库提供的 。

就像是:

    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);
        }
    });
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

当前 ( 1.3.0-alpha02) 没有更改半径的方法,但您可以在项目 ( dimens.xml) 中覆盖此尺寸:

<dimen name="mtrl_badge_with_text_radius">12dp</dimen>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


meh*_*avi -2

尝试这个改变:

<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>
Run Code Online (Sandbox Code Playgroud)