Android adds a prompt for the number of unread messages to the icon of the application (the number in the red circle)

Recently, we are working on a function to view unread messages. We need to add a function to prompt unread messages in the upper right corner of the tab icon of the tab page in the interface.

First, the last rendering is shown, which is more intuitive and understands the requirements:

There seem to be two ways of thinking:

1. Directly arrange the pictures of the base map and the red circle with relative layout, dynamically change the number of textview in the red circle in the code, and identify the display or hiding of the red circle in various cases. This method is more intuitive.

2. Draw circles and numbers with canvas.

Because the project adopts the form of tab page, the RadioButton in it is not suitable for relative layout. So I adopted the second way. The drawing process is written as a tool method. Called when needed. Facilitate future technology migration.

Direct code:

Call statement:

Note:

1. Count is the number of unread messages.

2. Setcomposunddrawableswithinterinsicbounds (drawable left, drawable top, drawable right, drawable bottom) can set icons on the top, bottom, left and right. If you don't want to display them in a certain place, set it to null. The width and height of the icon will be set to the inherent width and height, which is automatically obtained through getintrinsicwidth and getintrinsicheight. This method has the same effect as setting Android: drawabletop = "@ drawable / icon_mine" in XML.

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>