Android:是否可以用数字更新 ImageView/ImageButton 以显示新消息的数量?
在 iOS 中,我们有一个功能,可以通过在图标的右上角显示一个小数字来为用户更新应用程序图标,并添加新的待处理消息,类似地,我想知道我们是否有方法来更新 ImageView/ImageButton在android中(这里我不想更新主屏幕上的图标,而是更新我的应用程序内的图标)。
例如,该图片显示红色背景图标,其中以红色显示待读取的消息数量,如果没有消息,则显示为灰色背景。
谢谢,
In iOS we have a feature to update the app icon with the new pending messages for the user by showing a small number on the right-top corner of the icon and similarly I would like to know if we have method to update a ImageView/ImageButton in android(Here I am not looking to update the icon on the home screen but inside my app).
For example, the pic shows the red background icon with the number of messages pending to be read in red and if there are no messages it shows with a gray background.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对这个很感兴趣,因为我也不知道怎么做。所以我开始研究它,这就是我的做法。
icon.png
res/drawable/shapecount.xml
res/layout/ my_widget_layout.xml
主页小部件实际上是一个普通视图,您可以像在 Activity 中一样在 XML 文件中构建它。但有一些规则需要遵循。请参阅此链接获取指南
在此链接向您展示了如何构建AppWidget,您可以看到以下代码
:
R.layout.appwidget_provider_layout
是您的主页小部件的布局,它将是my_widget_layout.xml
从该
views
中,您可以设置您的值想要任何计数文本视图:然后您只需更新小部件本身:
注意:
updatePeriodMillis 不允许小部件每 30 分钟请求更新一次以上,因此我使用组合BroadcastReceiver 和服务
编辑:
如果您想要的计数应该在
Activity
而不是AppWidget
内,请参阅下面的活动测试。它使用与上面相同的 XML:I was interested in this SO as I didnt know how to do it neither. So I started looking into it and here is how I did it.
icon.png
from the sdkres/drawable/shapecount.xml
res/layout/my_widget_layout.xml
An home widget is actually a normal view that you can build in an XML file like you would for an activity. There is some rules to follow though. See this link for guideline
In this link which shows you how to build an AppWidget, you can see the following code:
where
R.layout.appwidget_provider_layout
is the layout of your home widget, which will bemy_widget_layout.xml
From that
views
, you can set the value you want for any count text view:and then you just have to update the widget itself:
NOTE:
The updatePeriodMillis doesnt allow the widget to request an update more than once every 30 min so I use a combination of BroadcastReceiver and Service
EDIT:
See below a activity test if the count you want should be within an
Activity
instead of aAppWidget
. It uses the same XMLs as above:当您希望在应用程序内部而不是在主屏幕上执行此操作时,我建议您只创建一个 ImageView 子类,并重写 onDraw() - 执行任何背景绘制,调用 super.onDraw(),然后执行前景绘画。
或者,您可以使用 Bitmap.copy 等从基本图标构建合成图像,以及带有消息计数的较小位图。
希望这有帮助,
菲尔·莱洛
As you're looking to do this inside your app, not on the home screen, I'd suggest you just create an ImageView subclass, and override onDraw() - do any background painting, call super.onDraw(), then do foreground painting.
Alternatively, you could use Bitmap.copy etc. to build a composite image from the base icon, and a smaller bitmap with the message count.
Hope this helps,
Phil Lello