ActionItemBadge:在actionbar上顯示badge數字提示
介紹:
一個方便你讓你在actionbar上顯示數字提示的庫(這種效果稱為badge )。其實現原理是利用了menu菜單資源文件屬性actionLayout
運行效果:

使用說明:
按照正常方式創建一個menu.xml ,同事需要添加actionLayout
,為了總是讓這個菜單項顯示出來,添加上showAsAction=
"always"
。
1
2
3
4
5
|
<item android:id= "@+id/item_samplebadge" android:actionLayout= "@layout/menu_badge" android:showAsAction= "always" android:title= "@string/sample_1" /> |
activity中
重寫onCreateOptionsMenu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); //you can add some logic (hide it if the count == 0) if (badgeCount > 0) { ActionItemBadge.update( this , menu.findItem(R.id.item_samplebadge), Iconify.IconValue.fa_android, ActionItemBadge.BadgeStyle.DARKGREY, badgeCount); } else { ActionItemBadge.hide(menu.findItem(R.id.item_samplebadge)); } //If you want to add your ActionItem programmatically you can do this too. You do the following: new ActionItemBadge.Add().act( this ).menu(menu).title(R.string.sample_2).itemDetails(0, SAMPLE2_ID, 1).showAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS).build(ActionItemBadge.BadgeStyle.BLUE_LARGE, 1); return true ; } |
如果你想在用戶點擊菜單項之后重新更新badge顯示的數目,那么在onOptionsItemSelected
中調用invalidateOptionsMenu
方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
@Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.item_samplebadge) { Toast.makeText( this , R.string.sample_3, Toast.LENGTH_SHORT).show(); badgeCount--; invalidateOptionsMenu(); return true ; } else if (id == SAMPLE2_ID) { Toast.makeText( this , R.string.sample_4, Toast.LENGTH_SHORT).show(); } return super .onOptionsItemSelected(item); } |
源碼下載:http://jcodecraeer.com/a/opensource/2014/1105/1910.html
本文由用戶 RichardUmf 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!