[英]dynamic drawable icon for ActionBar menu item? (Android, ActionBarSherlock)
How can I dynamically update the icon for an ActionBar MenuItem to show a 'red badge' with a number? 如何动态更新ActionBar MenuItem的图标以显示带有数字的“红色徽章”? (conceptually similar to "unread message count')
(概念上类似于“未读邮件计数”)
I'm using ActionBarSherlock in my Android application, targeting API level 10+. 我在Android应用程序中使用ActionBarSherlock,定位API级别为10+。 My application is for mobile data capture and, sometimes, the user cannot immediately submit new data but must save it locally on the phone (eg when connectivity is poor).
我的应用程序用于移动数据捕获,有时用户无法立即提交新数据,而必须将其本地保存在电话中(例如,连接不良时)。
Whenever there are 'unsubmitted' items stored locally on the phone, I'd like to show the user a menu item (in the Action Bar) so that they can click over to the UnsubmittedItems activity and trigger an upload manually. 每当电话本地存储有“未提交”的项目时,我想向用户显示一个菜单项(在操作栏中),以便他们可以单击到UnsubmittedItems活动并手动触发上传。
Ideally , this menu item will have some graphical representation of the number of unsubmitted items (similar to how an email or sms app shows the number of unread messages as a badge). 理想情况下 ,此菜单项将以图形方式表示未提交的项目数(类似于电子邮件或短信应用如何将未读消息的数量显示为标记)。 Currently, I'm just doing:
目前,我只是在做:
unsubmittedMenuItem.setTitle( Integer.toString(numUnsubmitted) );
And this works, but two issues: I'd prefer to use the Icon and keep the Title as something intelligible ("Unsubmitted") and, also, I'd love to have some custom layout or design here, such as a red circle background behind the number. 但这可行,但是有两个问题:我更喜欢使用图标并将标题保持为可理解的名称(“未提交”),而且,我希望在此处具有一些自定义的布局或设计,例如红色圆圈数字后面的背景。
Thoughts on how to accomplish this? 关于如何做到这一点的想法? So far my research suggest two possibilities:
到目前为止,我的研究提出了两种可能性:
These are similar in the sense that I'm rendering the text into a bitmap (either offline like #1 or on the fly like #2) but it would be great to leverage the 'native' text rendering of the ActionBar if possible and simply insert a red circle behind it like in a TextView. 从某种意义上说,这些是相似的,我正在将文本渲染为位图(像#1一样离线或像#2一样动态),但如果可能的话,最好利用ActionBar的“本机”文本渲染像在TextView中一样在其后插入一个红色圆圈。
Is there some way to set a custom layout just for the icon drawable? 有什么方法可以只为可绘制图标设置自定义布局吗? (NOT a full actionLayout, just for the icon?)
(不是完整的actionLayout,仅用于图标?)
There are a lot of ways to do this: 有很多方法可以做到这一点:
LayerDrawable
and stack badge images on top of your icon image. LayerDrawable
并将徽章图像堆叠在图标图像之上。 Drawable
and draws the icon and badge manually. Drawable
扩展的自定义类,并手动绘制图标和徽章。 ImageView
with a TextView
overlay for the badge. TextView
覆盖层的ImageView
的徽章中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.