简体   繁体   English

在Android 5.0棒棒糖中制作带有徽章/计数的工具栏通知图标

[英]make toolbar notification icon with badge/count in android 5.0 lollipop

How to add badge/count to notification in android 5.0 toolbar. 如何在Android 5.0工具栏中向通知添加徽章/计数。 I tried below code and it is working on 4.4. 我试过下面的代码,它正在4.4上工作。 But when i use toolbar, it gives me NPE on countView. 但是当我使用工具栏时,它在countView上给了我NPE。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.dashboad, menu);

    final View menuWork = menu.findItem(R.id.action_work).getActionView();
    countView = (TextView) menuWork.findViewById(R.id.actionbar_notification_textview);

    ImageButton notifyLayout = (ImageButton) menuWork.findViewById(R.id.img);
    menuWork.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    return true;
}

xml code for menu item 菜单项的xml代码

<item
    android:id="@+id/action_work"
    android:actionLayout="@layout/notification_alert"
    android:icon="@android:drawable/ic_popup_reminder"
    android:orderInCategory="100"
    android:title="Job"
    app:showAsAction="always" />

Just replace: 只需替换:

android:actionLayout="@layout/notification_alert"

With: 带有:

 app:actionLayout="@layout/notification_alert"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM