Android Notification Dynamic Icon

2019-06-16 04:18发布

问题:

Is it possible to overlay a notification icon with some text/int? or is it possible to generate a custom icon on the fly?

For example I have the current icon displayed for my notification, I would like to be able to display for example the number 2 within the red box.

As this is not a count of notifications ( so not the red bubble ), the setting the "number" in the notification will not work.

Will I have to just create an individual icon for every possible number I wish to display? Or is there a better way of achieving this?

回答1:

Try using AnimationDrawable's. You can use them to combine multiple drawables into a single one, and select the one you want to display. I really think, this is the way to go.

More info: AnimationDrawable

Off course, you will have to provide individual drawables for all your Integers you want to display, but this way, you can easily change the appearance of your notifications



回答2:

Notification.Builder

Has two methods:

setLargeIcon(Bitmap icon)

and

setSmallIcon(int icon)

Since the small Icon only seems to take a resource ID I think you'd have to provide a drawable resource for every possible number you want then you could select which one to use at run time.

The Large icon however takes a Bitmap, so you should be able to generate one with some java code and pass it in.

Truthfully though I don't exactly know what the difference is between the Large and Small icons. I don't have much experiences with Notifications in general. You might luck out and find that setLargeIcon will work for what you need. But it might also be that you can only use a Bitmap object for the image that is shown when the notification bar is pulled down, not the one that is shown while it is collapsed.