android - float 操作按钮上的徽章计数

标签 android android-layout

我想在 android 中显示计数徽章 float 操作按钮前面。我使用 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>

我在FAB下面得到了计数徽章,如下图

enter image description here

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

最佳答案

您可以使用 BadgeDrawable由 Material 组件库提供。

类似于:

    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);
        }
    });

enter image description here

目前 (1.3.0-alpha02) 没有改变半径的方法,但是你可以在你的项目中覆盖这个维度 (dimens.xml) :

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

enter image description here

关于android - float 操作按钮上的徽章计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37639698/

相关文章:

android - 如何使用数据绑定(bind)库将自定义对象绑定(bind)到微调器布局?

android - 如何在 android 中使用社交身份验证访问 facebook 好友列表?

android - R.java 权限被拒绝使用旧工作区时 Eclipse 中的错误

android - 自定义微调器适配器 simple_spinner_dropdown_item 显示不正确

android - 如何将广告 View 与游戏 View 分开?

Android:当acitvity支持横向和纵向时如何设置默认方向

java - 在 ViewRootImpl.java 中创建线程

android - 启用安卓 :forceDarkMode programmatically?

java - android:图标中的图像未显示

java - 在android中使用Fragment标签时出现XML错误