像谷歌动作条通知计数徽章 [英] Actionbar notification count badge like Google has
问题描述
有没有一款Android标准徽章或方法来显示操作栏通知图标有像谷歌的例子计数?
Is there a android-standard badge or method to show action-bar notification icon with a count like on Google examples?
如果不,比什么是让它的最佳方法是什么?
我是新来的Android,请大家帮忙。
If not, than what is the best way to make it?
I'm new to android, please help.
推荐答案
我不知道这是否是最好的解决办法或没有,但它是我所需要的。
I am not sure if this is the best solution or not, but it is what I need.
请告诉我,如果你知道的是需要更好的性能或质量要改变什么。就我而言,我有一个按钮。
Please tell me if you know what is need to be changed for better performance or quality. In my case, I have a button.
我的菜单上的自定义项 - 的main.xml
Custom item on my menu - main.xml
<item
android:id="@+id/badge"
android:actionLayout="@layout/feed_update_count"
android:icon="@drawable/shape_notification"
android:showAsAction="always">
</item>
自定义形状绘制(背景方) - shape_notification.xml
Custom shape drawable (background square) - shape_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:color="#22000000" android:width="2dp"/>
<corners android:radius="5dp" />
<solid android:color="#CC0001"/>
</shape>
布局我的看法 - feed_update_count.xml
Layout for my view - feed_update_count.xml
<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/notif_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="32dp"
android:minHeight="32dp"
android:background="@drawable/shape_notification"
android:text="0"
android:textSize="16sp"
android:textColor="@android:color/white"
android:gravity="center"
android:padding="2dp"
android:singleLine="true">
</Button>
MainActivity - 设置和更新我的看法
MainActivity - setting and updating my view
static Button notifCount;
static int mNotifCount = 0;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.main, menu);
View count = menu.findItem(R.id.badge).getActionView();
notifCount = (Button) count.findViewById(R.id.notif_count);
notifCount.setText(String.valueOf(mNotifCount));
return super.onCreateOptionsMenu(menu);
}
private void setNotifCount(int count){
mNotifCount = count;
invalidateOptionsMenu();
}
这篇关于像谷歌动作条通知计数徽章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!