徽章计数浮动动作按钮

发布于 2025-02-12 22:39:44 字数 1484 浏览 2 评论 0原文

我想在Android中的浮动动作按钮的面前显示徽章。我使用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下方获得了计数徽章,如下所示,如下所示

“在此处输入图像说明”

我需要计数徽章才能在晶圆厂。

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

   <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>

I get the count badge below the FAB, as shown below

enter image description here

I need the count badge to be in front of the FAB.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

歌入人心 2025-02-19 22:39:44

您可以使用 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);
        }
    });

在此处输入图像描述“

当前(1.3.0-Alpha02)半径,但是您可以在项目中覆盖此尺寸(dimens.xml):

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

“在此处输入图像说明”

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

enter image description here

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>

enter image description here

纵情客 2025-02-19 22:39:44

对于此问题,解决方案是Android:hevation =“ 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>

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

enter image description here

<?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>
梦一生花开无言 2025-02-19 22:39:44

我使用 counterfab库实现这一目标。

它与将其添加到XML布局文件中一样简单,而不是常规floatingActionButton和调用counterfab.setCount(10);(请参阅使用示例在这里)。

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).

眼中杀气 2025-02-19 22:39:44

正如丹尼尔(Daniel)所建议的那样,您不应在浮动动作按钮中添加徽章。但是,如果您确实必须这样做,请尝试将此属性添加到您的文本视图中:

android:elevation="7dp"

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"
深巷少女 2025-02-19 22:39:44

可以通过提供自定义样式的浮动样式来简单地使用textView

        <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" />

“结果”

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" />

Result

时光匆匆的小流年 2025-02-19 22:39:44

尝试一下:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top|left"
    app:layout_anchor="@+id/containerpager"
    app:layout_anchorGravity="bottom|right">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:elevation="0dp"
        android:visibility="visible"
        app:backgroundTint="@color/colorPrimary"
        app:borderWidth="0dp"
        app:fabSize="normal"
        app:srcCompat="@drawable/ic_review_order" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="5"
        android:textColor="@color/white"
        android:textSize="22sp"
        android:visibility="visible" />
</RelativeLayout>

Give it a Try :

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="top|left"
    app:layout_anchor="@+id/containerpager"
    app:layout_anchorGravity="bottom|right">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="@dimen/fab_margin"
        android:elevation="0dp"
        android:visibility="visible"
        app:backgroundTint="@color/colorPrimary"
        app:borderWidth="0dp"
        app:fabSize="normal"
        app:srcCompat="@drawable/ic_review_order" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="5"
        android:textColor="@color/white"
        android:textSize="22sp"
        android:visibility="visible" />
</RelativeLayout>
榆西 2025-02-19 22:39:44

你可能想尝试这个

<FrameLayout
    android:layout_gravity="bottom|end"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <com.nex3z.notificationbadge.NotificationBadge
        android:id="@+id/badge"
        android:layout_width="28dp"
        android:layout_height="28dp"
        android:layout_gravity="right|top"
        android:elevation="7dp"
        app:nbMaxTextLength="2" />


    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginRight="15dp"
        android:src="@drawable/ic_shopping_cart_black_24dp"
        app:backgroundTint="@color/colorprice" />
</FrameLayout>    

you might wanna try this

<FrameLayout
    android:layout_gravity="bottom|end"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <com.nex3z.notificationbadge.NotificationBadge
        android:id="@+id/badge"
        android:layout_width="28dp"
        android:layout_height="28dp"
        android:layout_gravity="right|top"
        android:elevation="7dp"
        app:nbMaxTextLength="2" />


    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginRight="15dp"
        android:src="@drawable/ic_shopping_cart_black_24dp"
        app:backgroundTint="@color/colorprice" />
</FrameLayout>    
巷雨优美回忆 2025-02-19 22:39:44

我已经使用了约束布局,希望它对
你们任何人。

    <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/cvFilterBtnFragDiscover"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:visibility="gone">
    
                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/ivFilterFragDiscover"
                    android:layout_width="@dimen/_40sdp"
                    android:layout_height="@dimen/_40sdp"
                    android:layout_gravity="bottom|end"
                    android:layout_margin="16dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:srcCompat="@drawable/ic_filter_recommended"
                    tools:ignore="ContentDescription" />
    
                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/tvConnCountToolbarWithTab"
                    android:layout_width="@dimen/_8sdp"
                    android:layout_height="@dimen/_8sdp"
                    android:background="@drawable/ic_dot"
                    app:layout_constraintCircle="@+id/ivFilterFragDiscover"
                    app:layout_constraintCircleAngle="35"
                    app:layout_constraintCircleRadius="@dimen/_18sdp"
                    app:layout_constraintStart_toEndOf="@+id/ivFilterFragDiscover"
                    app:layout_constraintTop_toTopOf="@+id/ivFilterFragDiscover" />
            </androidx.constraintlayout.widget.ConstraintLayout>

I have managed using constrain layout , I hope it will be helpful to
any of you .

    <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/cvFilterBtnFragDiscover"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:visibility="gone">
    
                <androidx.appcompat.widget.AppCompatImageView
                    android:id="@+id/ivFilterFragDiscover"
                    android:layout_width="@dimen/_40sdp"
                    android:layout_height="@dimen/_40sdp"
                    android:layout_gravity="bottom|end"
                    android:layout_margin="16dp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:srcCompat="@drawable/ic_filter_recommended"
                    tools:ignore="ContentDescription" />
    
                <androidx.appcompat.widget.AppCompatTextView
                    android:id="@+id/tvConnCountToolbarWithTab"
                    android:layout_width="@dimen/_8sdp"
                    android:layout_height="@dimen/_8sdp"
                    android:background="@drawable/ic_dot"
                    app:layout_constraintCircle="@+id/ivFilterFragDiscover"
                    app:layout_constraintCircleAngle="35"
                    app:layout_constraintCircleRadius="@dimen/_18sdp"
                    app:layout_constraintStart_toEndOf="@+id/ivFilterFragDiscover"
                    app:layout_constraintTop_toTopOf="@+id/ivFilterFragDiscover" />
            </androidx.constraintlayout.widget.ConstraintLayout>
明月松间行 2025-02-19 22:39:44

尝试此更改:

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文