使用 FloatingSearchView 在操作菜单栏上显示徽章 android

badge over actionmenu bar using FloatingSearchView android

我正在为搜索栏使用 arimorty/floatingsearchview 库,它工作正常,但现在我想要一个带有红色 circle/badge 的通知图标,但无法找到实现它的方法。

在activity中:

<com.arlib.floatingsearchview.FloatingSearchView
        android:id="@+id/floating_search_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:floatingSearch_searchHint="Search..."
        app:floatingSearch_suggestionsListAnimDuration="250"
        app:floatingSearch_showSearchKey="false"
        app:floatingSearch_leftActionMode="showHamburger"
        app:floatingSearch_menu="@menu/searchbar_sidemenu"
        app:floatingSearch_close_search_on_keyboard_dismiss="true"
        android:paddingEnd="10dp"
        android:paddingStart="10dp"
        android:paddingTop="10dp"/>

菜单代码:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_notification"
        android:title="Notification"
        app:showAsAction="ifRoom"
        android:icon="@drawable/ic_notifications_none_black_24dp"/>
</menu>

我尝试了其他答案,但 none 对我有用。

如果需要,我会 explain/clarify 或分享 java 代码。

[更新]

我试图实现这个 answer 和其他答案。

在 GitHub 存储库中搜索并找到 this 但不明白。

更改您的搜索图标使用此:

myIcon.setDrawable(setBadgeCount(context, R.drawable.ic_shopping_cart_primary, 1));

使用此 class 更改带有 badgedDrawable 的可绘制对象:

public static Drawable setBadgeCount(Context context, int res, int badgeCount) {
    LayerDrawable icon = (LayerDrawable) ContextCompat.getDrawable(context, R.drawable.ic_badge_drawable);
    Drawable mainIcon = ContextCompat.getDrawable(context, res);
    BadgeDrawable badge = new BadgeDrawable(context);
    badge.setCount(String.valueOf(badgeCount));
    assert icon != null;
    icon.mutate();
    icon.setDrawableByLayerId(R.id.ic_badge, badge);
    icon.setDrawableByLayerId(R.id.ic_main_icon, mainIcon);
    return icon;
}

创建这个 class:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
import androidx.annotation.NonNull;

public class BadgeDrawable extends Drawable {

    private Paint mBadgePaint;
    private Paint mBadgePaint1;
    private Paint mTextPaint;
    private Rect mTxtRect = new Rect();

    private String mCount = "";
    private boolean mWillDraw = false;


    public BadgeDrawable(Context context) {
        float mTextSize = dpToPx(context); //text size
        mBadgePaint = new Paint();
        mBadgePaint.setColor(Color.RED);
        mBadgePaint.setAntiAlias(true);
        mBadgePaint.setStyle(Paint.Style.FILL);
        mBadgePaint1 = new Paint();
        mBadgePaint1.setColor(Color.parseColor("#EEEEEE"));
        mBadgePaint1.setAntiAlias(true);
        mBadgePaint1.setStyle(Paint.Style.FILL);

        mTextPaint = new Paint();
        mTextPaint.setColor(Color.WHITE);
        mTextPaint.setTypeface(Typeface.DEFAULT);
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
    }

    private float dpToPx(Context context) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) 8, context.getResources().getDisplayMetrics());
    }


    @Override
    public void draw(@NonNull Canvas canvas) {
        if (!mWillDraw) {
            return;
        }
        Rect bounds = getBounds();
        float width = bounds.right - bounds.left;
//        float height = bounds.bottom - bounds.top;
        // Position the badge in the top-right quadrant of the icon.

        /*Using Math.max rather than Math.min */
        //        float radius = ((Math.max(width, height) / 2)) / 2;
        float radius = width * 0.15f;
        float centerX = (width - radius - 1) +10;
        float centerY = radius -5;
        if(mCount.length() <= 2){
            // Draw badge circle.
            canvas.drawCircle(centerX, centerY, radius+9, mBadgePaint1);
            canvas.drawCircle(centerX, centerY, radius+7, mBadgePaint);
        }
        else {
            canvas.drawCircle(centerX, centerY, radius+10, mBadgePaint1);
            canvas.drawCircle(centerX, centerY, radius+8, mBadgePaint);
        }
        // Draw badge count text inside the circle.
        mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect);
        float textHeight = mTxtRect.bottom - mTxtRect.top;
        float textY = centerY + (textHeight / 2f);
        if(mCount.length() > 2)
            canvas.drawText("99+", centerX, textY, mTextPaint);
        else
            canvas.drawText(mCount, centerX, textY, mTextPaint);
    }

    /*
     Sets the count (i.e notifications) to display.
      */
    public void setCount(String count) {
        mCount = count;
        // Only draw a badge if there are notifications.
        mWillDraw = !count.equalsIgnoreCase("0");
        invalidateSelf();
    }

    @Override
    public void setAlpha(int alpha) {
        // do nothing
    }

    @Override
    public void setColorFilter(ColorFilter cf) {
        // do nothing
    }

    @Override
    public int getOpacity() {
        return PixelFormat.UNKNOWN;
    }
}

setCount 中将 mWillDraw = !count.equalsIgnoreCase("0"); 更改为 true 如果你想显示没有数字

结果:

ic_badge_drawable.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/ic_main_icon"
        android:drawable="@drawable/ic_shopping_cart_primary"
        android:gravity="center" />


    <!-- set a place holder Drawable so android:drawable isn't null -->
    <item android:id="@+id/ic_badge"
        android:drawable="@drawable/ic_shopping_cart_primary" />
</layer-list>

将ic_shopping_cart_primary替换为任何drawable,这不是用的,只是为了默认

Review codes and change setCount to pass " " (space)

感谢

这里是详细答案。

  1. 下载 arimorty/floatingsearchview and import it as module () 如果您正在使用 java,您可以从下载的 library 文件夹中导入。

  2. 导入后打开MenuView文件src->main->java->com.arlib.floatingsearchview->util->view->MenuView.java 在此文件中找到 ImageView action = createActionView();action 更改为全局字段

在 MenuView 中创建一个新方法

public void chngNotificationIcon(int ResID){
 action.setImageResource(ResID);
}
  1. 打开 FloatingSearchView.java 文件并创建此方法

    public void chngNotificationIcon(int ResID){ mMenuView.chngNotificationIcon(ResID); }

  2. 在您的主项目中创建一个新的 drawbale 用于带有红色圆圈的通知,并将其命名为 notification_withdot.xml

现在您可以使用

设置任何图标
floatingSearchViewObject.chngNotificationIcon(R.drawable.notification_withdot);

如果您遇到任何错误,请告诉我。