Adding badge to tab in android

后端 未结 5 1687
生来不讨喜
生来不讨喜 2021-02-04 21:38

i want to add badge to tab in my app as in iPhone.

Screen shot of badge used in iPhone is in the following link:

5条回答
  •  无人及你
    2021-02-04 22:18

    I also looked for a solution for this in the android sdk and found none so i went and wrote a custom TabWidget for it.

    The short version:

    • Extend the TabWidget class and override the method: drawChild (Canvas canvas, View child, long drawingTime)
    • In the painting you simply draw a Bitmap ontop of the canvas. You get the x location of the child with child.getRight() and subtract the width of your badge Bitmap. Also draw the text for the badge number ontop of that.
    • To keep a state for each tab you could use a HashMap with keys for the index of each tab.
    • To be able to update the tabs from anywhere i used a singleton class with the widget inside that i initialize from the main activity.

    More info can be found here, including the source code for an example android project:

    http://nilisoft.com/?p=622

    Enjoy!

提交回复
热议问题