如何在工具栏MenuItem图标上添加徽章 [英] How to add badges on Toolbar MenuItem Icons

查看:125
本文介绍了如何在工具栏MenuItem图标上添加徽章的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图为自己找到答案,但找不到.

I tried to find an answer for myself but couldn't find it.

我需要在工具栏的MenuItem图标上制作徽章,像这样:

I need make badge on the MenuItem icon in the Toolbar, like this:

我该怎么做?

推荐答案

以下是逐步功能:

添加menu.xml

add menu.xml

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto">

         <item
              android:id="@+id/actionNotifications"
              android:icon="@drawable/ic_info_outline_white_24dp"
              android:menuCategory="secondary"
              android:orderInCategory="1"
              android:title="Cart"
              app:actionLayout="@layout/notification_layout"
              app:showAsAction="always" />
    </menu>

然后添加notification_layout.xml,此布局将用作通知图标布局

Then add notification_layout.xml, this layout will be used as the notification icons layout

     <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        style="@android:style/Widget.ActionButton"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center"
        android:background="@android:color/transparent"
        android:clickable="true"
        android:gravity="center"
        android:orientation="vertical">

     <ImageView
         android:id="@+id/hotlist_bell"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_margin="0dp"
         android:contentDescription="Notification Icon"
         android:gravity="center"
         android:src="@drawable/ic_info_outline_white_24dp" />

     <TextView xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/txtCount"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_marginBottom="@dimen/x5dp"
         android:layout_marginLeft="@dimen/x10dp"
         android:layout_marginRight="0dp"
         android:background="@drawable/pointer_"
         android:gravity="center"
         android:minWidth="17sp"
         android:text="0"
         android:textColor="#ffffffff"
         android:textSize="12sp" />
    </RelativeLayout>

现在在活动中

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

    final View notificaitons = menu.findItem(R.id.actionNotifications).getActionView();

    txtViewCount = (TextView) notificaitons.findViewById(R.id.txtCount);
    updateHotCount(count++);
    txtViewCount.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            updateHotCount(count++);
        }
    });
    notificaitons.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
    //    TODO
        }
    });


    return true;
}

您可以将以下函数(从stackoverflow中获取)放入活动中以更新计数器:

You can put following function (taken from stackoverflow) inside the activity to update counter:

    public void updateHotCount(final int new_hot_number) {
    count = new_hot_number;
    if (count < 0) return;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (count == 0)
                txtViewCount.setVisibility(View.GONE);
            else {
                txtViewCount.setVisibility(View.VISIBLE);
                txtViewCount.setText(Integer.toString(count));
                // supportInvalidateOptionsMenu();
            }
        }
    });
}      

这篇关于如何在工具栏MenuItem图标上添加徽章的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆