像谷歌这样的操作栏通知计数图标(徽章) [英] Actionbar notification count icon (badge) like Google has
问题描述
是否有 Android 标准徽章或方法来显示操作栏通知图标和 Google 示例中的计数一样?
Is there a android standard badge or method to show action bar notification icon with a count like on Google examples?
如果没有,那么最好的方法是什么?
我是安卓新手,请帮忙.
If not, then 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屋!