带有通知计数的收费栏

Tollbar with a notification count

你好,我正在使用收费栏,我在里面的菜单选项中添加了一些可绘制的项目。我想要的是在那些可绘制对象中放入一个数字。喜欢下图

View post on imgur.com

我知道还有其他 post 与此相关但与工具栏无关,我需要的是工具栏。

谢谢

最简单的方法就是使用星星背景的TextView:

<TextView
    android:id="@+id/notification"
    android:layout_width="@dimen/notification_width"
    android:layout_height="@dimen/notification_height"
    android:background="@drawable/bg_star"
    android:gravity="center"
    android:textColor="@android:color/white"
    tools:text="5" />

您可以使用明显的 TextView 的 setText() 方法来设置或隐藏通知文本。

要将此布局添加到您的工具栏,请使用 Android 菜单的 app:actionLayout 参数:

<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">
    <item
        android:id="@+id/action_notifications"
        app:actionLayout="@layout/view_notification"
        app:showAsAction="always" />
</menu>

然后在您的 Activity 中像这样扩充菜单:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.notification_menu, menu);
    return true;
}
   <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_top"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="@color/action_bar_bkgnd"
        app:theme="@style/ToolBarTheme" >


         <TextView


            android:background="@drawable/bg_star"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Toolbar Title"
            android:id="@+id/notification"
            android:layout_gravity="center"
            android:id="@+id/toolbar_title" />


        </android.support.v7.widget.Toolbar>


       Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
       TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);