我如何动态更新图标的动作条菜单项显示一个红色徽章有多少? (概念上类似于未读邮件数')
How can I dynamically update the icon for an ActionBar MenuItem to show a 'red badge' with a number? (conceptually similar to "unread message count')
我用ActionBarSherlock在我的Android应用程序,针对API级别10+。我的应用是移动数据采集,有时,用户不能马上提交新的数据,但必须在本地保存在手机上(例如,当连通性较差)。
I'm using ActionBarSherlock in my Android application, targeting API level 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 (e.g. when connectivity is poor).
每当有本地存储在手机上未提交的项目,我想向用户显示一个菜单项(操作栏中的),这样他们可以单击切换到UnsubmittedItems活动,并手动进行上传。
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.
理想,该菜单项将未提交的项目数的某些图形重新presentation(类似于如何电子邮件或SMS应用程序显示未读邮件的徽章的数量)。目前,我只是在做:
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) );
和这个工作,但有两个问题:我想preFER使用的图标,并保持标题的东西理解的(未提交),并也,我很想有一些自定义的布局和设计在这里诸如红色圆圈背景后面的数字。
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:
使用一个级别列表绘制是pcreated一段范围内的数字(如1,2,3 ... 10+)$ P $然后unsubmittedMenuItem.getIcon()。执行setLevel(numUnsubmitted) 建立整个位图完全是动态的,使用位图和Canvas API的这些都是在我呈现文本为位图的意义相似的(无论是离线像#1或类似飞#2),但是这将是巨大的,如果利用该动作条的原生的文本渲染可能的,只需插入其背后的红色圆圈像一个TextView。
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.
有没有一些方法来设置自定义布局只是为了图标绘制? (不是一个完整的actionLayout,只是图标?)
Is there some way to set a custom layout just for the icon drawable? (NOT a full actionLayout, just for the icon?)
有很多方法可以做到这一点:
There are a lot of ways to do this:
使用LayerDrawable和堆栈徽章图片上你的图标图像的顶部。 在写这从绘制对象
延伸和手动绘制图标和徽章的自定义类。
使用自定义操作项视图中的的ImageView
与的TextView
覆盖的徽章。
Use LayerDrawable
and stack badge images on top of your icon image.
Write a custom class which extends from Drawable
and draws the icon and badge manually.
Use a custom action item view with your icon in an ImageView
with a TextView
overlay for the badge.
上一篇:如何解决错误[否资源的发现,匹配给定的名称和QUOT;添加库V7 AppCompat当在Eclipse?如何解决、错误、名称、发现
下一篇:安卓:java.lang.OutOfMemoryError:java、lang、OutOfMemoryError