我想是这样的:
第三图标的通知,它仅仅是一个PNG图像了。是否有可能做一些事情,这样我可以更改文本/数即...,03编程,以显示实际NO.OF通知。
The 3rd icon is for notifications and it is just a png image now. Is it possible to do something, so that i can change the text/number ie.., 03 programatically to show the actual no.of notifications.
感谢您
这是为我工作的一些例如code。
Here is some example code that worked for me.
1:创建您的徽章菜单项的布局
1: Create a layout for your badge menu item.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="48dp"
android:layout_height="fill_parent"
android:layout_gravity="right" >
<!-- Menu Item Image -->
<ImageView
android:layout_width="48dp"
android:layout_height="fill_parent"
android:clickable="true"
android:src="@drawable/bkg_actionbar_notify_off" />
<!-- Badge Count -->
<TextView
android:id="@+id/actionbar_notifcation_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="@dimen/padding_small"
android:text="99"
android:textColor="@color/holo_orange_dark" />
</RelativeLayout>
2:创建在RES /菜单的菜单项,并设置actionLayout到您的布局
2: Create a menu item in res/menu and set the actionLayout to your layout
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/badge"
android:actionLayout="@layout/actionbar_badge_layout"
android:icon="@drawable/icn_menu_posts"
android:showAsAction="always">
</item>
</menu>
3:然后在你的活动或片段的onCreateOptionsMenu你可以做这样的事情...
3: Then in onCreateOptionsMenu of your activity or fragment you can do something like this...
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.badge, menu);
RelativeLayout badgeLayout = (RelativeLayout) menu.findItem(R.id.badge).getActionView();
TextView tv = (TextView) badgeLayout.findViewById(R.id.actionbar_notifcation_textview);
tv.setText("12");
}
请注意:如果你想改变以后的徽章数量,你可以存储一个引用传递给onCreateOptionsMenu菜单对象,并使用相同的code,以获得所需的视图,设置一个值。
Note: If you wanted to change the badge count later on, you could store a reference to the Menu object passed to onCreateOptionsMenu and use the same code to get the required view and set a value.
=== ApCompat警告========================================== ========
=== ApCompat Warning ==================================================
如果使用AppCompatActivity则必须设置而ActionView在格兰onCreateOptionsMenu
If using the AppCompatActivity then you must set the actionView in teh onCreateOptionsMenu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
MenuItem item = menu.findItem(R.id.badge);
MenuItemCompat.setActionView(item, R.layout.actionbar_badge_layout);
RelativeLayout notifCount = (RelativeLayout) MenuItemCompat.getActionView(item);
TextView tv = (TextView) notifCount.findViewById(R.id.actionbar_notifcation_textview);
tv.setText("12");
return super.onCreateOptionsMenu(menu);