我想使操作栏中的通知图标以通知的数量内。
例如(谷歌Adsence ):
我发现计算器这个答案,但它不能完全回答我的问题,因为在这种情况下,它是唯一的号码,与一些没有图标:的与数 解决方案
很多,所以我转向了博客尝试了几乎所有的资源后,成功地。我想分享什么工作对我来说(API> = 13); 源。
让我们先从甜code,的方式使用它
公共布尔onCreateOptionsMenu(功能菜单){
//膨胀菜单
。getMenuInflater()膨胀(R.menu.menu_my,菜单);
//获取通知的菜单项和LayerDrawable(层列表)
菜单项项= menu.findItem(R.id.action_notifications);
LayerDrawable图标=(LayerDrawable)item.getIcon();
//更新LayerDrawable的BadgeDrawable
Utils2.setBadgeCount(这一点,图标,2);
返回true;
}
在 menu_my.xml
:
<菜单的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
的xmlns:工具=http://schemas.android.com/tools
工具:上下文=MainActivity。>
<项目
机器人:ID =@ + ID / action_notifications
机器人:图标=@可绘制/ ic_menu_notifications
机器人:标题=通知
应用程序:showAsAction =总是/>
< /菜单>
本类方便地使 BadgeDrawable
:
公共类BadgeDrawable扩展绘制对象{
私人浮动mTextSize;
私人油漆mBadgePaint;
私人油漆mTextPaint;
私人矩形mTxtRect =新的矩形();
私人字符串mCount =;
私人布尔mWillDraw = FALSE;
公共BadgeDrawable(上下文的背景下){
// mTextSize = context.getResources()getDimension(R.dimen.badge_text_size)。
mTextSize = 12F;
mBadgePaint =新的油漆();
mBadgePaint.setColor(Color.RED);
mBadgePaint.setAntiAlias(真正的);
mBadgePaint.setStyle(Paint.Style.FILL);
mTextPaint =新的油漆();
mTextPaint.setColor(Color.WHITE);
mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
mTextPaint.setTextSize(mTextSize);
mTextPaint.setAntiAlias(真正的);
mTextPaint.setTextAlign(Paint.Align.CENTER);
}
@覆盖
公共无效画(油画画布){
如果(!mWillDraw){
返回;
}
矩形边界的getBounds =();
浮动宽度= bounds.right - bounds.left;
浮动高度= bounds.bottom - bounds.top;
//位置在图标的右上象限的徽章。
浮半径=((Math.min(宽度,高度)/ 2) - 1)/ 2;
浮动的centerX =宽度 - 半径 - 1;
浮centerY =半径+ 1;
//绘制徽章圈。
canvas.drawCircle(的centerX,centerY,半径,mBadgePaint);
//画出圆圈内的徽章数量的文本。
mTextPaint.getTextBounds(mCount,0,mCount.length(),mTxtRect);
浮textHeight不同= mTxtRect.bottom - mTxtRect.top;
浮textY处= centerY +(textHeight不同/ 2F);
canvas.drawText(mCount,的centerX,textY处,mTextPaint);
}
/ *
设置数(即通知)来显示。
* /
公共无效setCount(诠释计数){
mCount = Integer.toString(计数);
//只画一个徽章,如果有通知。
mWillDraw =计数> 0;
invalidateSelf();
}
@覆盖
公共无效setAlpha(INT阿尔法){
// 没做什么
}
@覆盖
公共无效setColorFilter(ColorFilter CF){
// 没做什么
}
@覆盖
公众诠释getOpacity(){
返回PixelFormat.UNKNOWN;
}
}
这个类可以帮助设定数值。
公共类Utils2 {
公共静态无效setBadgeCount(上下文的背景下,LayerDrawable图标,诠释计数){
BadgeDrawable徽章;
//重用绘制如果可能的话
绘制对象重用= icon.findDrawableByLayerId(R.id.ic_badge);
如果(再利用= NULL和放大器;!&安培;重新使用的instanceof BadgeDrawable){
徽章=(BadgeDrawable)重用;
} 其他 {
徽章=新BadgeDrawable(上下文);
}
badge.setCount(计数);
icon.mutate();
icon.setDrawableByLayerId(R.id.ic_badge,徽章);
}
}
和 MUI重要提示的被拉伸(如布局)在 RES /绘制
:
< XML版本=1.0编码=UTF-8&GT?;
<层列表的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目
机器人:ID =@ + ID / ic_notification
机器人:可绘制=@可绘制/ ice_skate
机器人:重力=中心/>
!< - 设置一个占位符,可绘制因此Android:绘制不为空 - >
<项目
机器人:ID =@ + ID / ic_badge
机器人:可绘制=@可绘制/ ice_skate/>
< /层列表>
好卢克斯!
I would like to make a notification icon in the action bar to inside the number of notifications.
For example (Google Adsence) :
I found this answer on stackoverflow, but it does not fully answer my question because in this case it is only the number and not an icon with a number: Actionbar notification icon with count
解决方案After a lot of trying of nearly all resources on SO I turned to blogs; succesfully. I want to share what worked for me (Api >= 13); source.
Let's start with the sweet code, the way it's used:
public boolean onCreateOptionsMenu(Menu menu) {
//inflate menu
getMenuInflater().inflate(R.menu.menu_my, menu);
// Get the notifications MenuItem and LayerDrawable (layer-list)
MenuItem item = menu.findItem(R.id.action_notifications);
LayerDrawable icon = (LayerDrawable) item.getIcon();
// Update LayerDrawable's BadgeDrawable
Utils2.setBadgeCount(this, icon, 2);
return true;
}
The menu_my.xml
:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".MainActivity">
<item
android:id="@+id/action_notifications"
android:icon="@drawable/ic_menu_notifications"
android:title="Notifications"
app:showAsAction="always" />
</menu>
This class that conveniently makes a BadgeDrawable
:
public class BadgeDrawable extends Drawable {
private float mTextSize;
private Paint mBadgePaint;
private Paint mTextPaint;
private Rect mTxtRect = new Rect();
private String mCount = "";
private boolean mWillDraw = false;
public BadgeDrawable(Context context) {
//mTextSize = context.getResources().getDimension(R.dimen.badge_text_size);
mTextSize = 12F;
mBadgePaint = new Paint();
mBadgePaint.setColor(Color.RED);
mBadgePaint.setAntiAlias(true);
mBadgePaint.setStyle(Paint.Style.FILL);
mTextPaint = new Paint();
mTextPaint.setColor(Color.WHITE);
mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);
mTextPaint.setTextSize(mTextSize);
mTextPaint.setAntiAlias(true);
mTextPaint.setTextAlign(Paint.Align.CENTER);
}
@Override
public void draw(Canvas canvas) {
if (!mWillDraw) {
return;
}
Rect bounds = getBounds();
float width = bounds.right - bounds.left;
float height = bounds.bottom - bounds.top;
// Position the badge in the top-right quadrant of the icon.
float radius = ((Math.min(width, height) / 2) - 1) / 2;
float centerX = width - radius - 1;
float centerY = radius + 1;
// Draw badge circle.
canvas.drawCircle(centerX, centerY, radius, mBadgePaint);
// Draw badge count text inside the circle.
mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect);
float textHeight = mTxtRect.bottom - mTxtRect.top;
float textY = centerY + (textHeight / 2f);
canvas.drawText(mCount, centerX, textY, mTextPaint);
}
/*
Sets the count (i.e notifications) to display.
*/
public void setCount(int count) {
mCount = Integer.toString(count);
// Only draw a badge if there are notifications.
mWillDraw = count > 0;
invalidateSelf();
}
@Override
public void setAlpha(int alpha) {
// do nothing
}
@Override
public void setColorFilter(ColorFilter cf) {
// do nothing
}
@Override
public int getOpacity() {
return PixelFormat.UNKNOWN;
}
}
This class that helps to set the number.
public class Utils2 {
public static void setBadgeCount(Context context, LayerDrawable icon, int count) {
BadgeDrawable badge;
// Reuse drawable if possible
Drawable reuse = icon.findDrawableByLayerId(R.id.ic_badge);
if (reuse != null && reuse instanceof BadgeDrawable) {
badge = (BadgeDrawable) reuse;
} else {
badge = new BadgeDrawable(context);
}
badge.setCount(count);
icon.mutate();
icon.setDrawableByLayerId(R.id.ic_badge, badge);
}
}
And mui importante a drawable (like a layout) in res/drawable
:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/ic_notification"
android:drawable="@drawable/ice_skate"
android:gravity="center" />
<!-- set a place holder Drawable so android:drawable isn't null -->
<item
android:id="@+id/ic_badge"
android:drawable="@drawable/ice_skate" />
</layer-list>
Good lucks!