6
A
回答
6
在我的解決方案,每當有新通知到來時,計數器將增加(如在購物應用觀察)
試試這個,它適用於我的MOTO e2。
確保您API等級> 14
創建這樣一個佈局:
</RelativeLayout>
<ImageView
android:id="@+id/counterBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/unread_background" />
<TextView
android:id="@+id/count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="8sp"
android:layout_centerInParent="true"
android:textColor="#FFFFFF" />
</RelativeLayout>
在onCreateOptionMenu
,
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem menuItem = menu.findItem(R.id.testAction);
menuItem.setIcon(buildCounterDrawable(count, R.drawable.ic_menu_gallery));
return true;
}
現在,構建方法圖標:
private Drawable buildCounterDrawable(int count, int backgroundImageId) {
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.counter_menuitem_layout, null);
view.setBackgroundResource(backgroundImageId);
if (count == 0) {
View counterTextPanel = view.findViewById(R.id.counterValuePanel);
counterTextPanel.setVisibility(View.GONE);
} else {
TextView textView = (TextView) view.findViewById(R.id.count);
textView.setText("" + count);
}
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.setDrawingCacheEnabled(true);
view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
return new BitmapDrawable(getResources(), bitmap);
}
你可以參照從here
2
你可以做我的菜單如下
定製項目 - 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
<?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
<?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 - 設置和更新我的觀點
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();
}
1
只要改變 android:actionLayout="@layout/feed_update_count"
到 app:actionLayout="@layout/feed_update_count"
。 它會工作
0
而不是創建形狀和drawable,您可以使用徽章樣式TextView
本身。按照您的主題使用自定義浮動按鈕樣式。
例 -
<TextView
android:id="@+id/fabCounter"
style="@style/Widget.Design.FloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="10dp"
android:padding="5dp"
android:text="-9"
android:textColor="@android:color/black"
android:textSize="14sp" />
相關問題
- 1. 如何在android 5.0工具欄中添加徽章/計數通知在android 5.0棒棒糖中使徽章/計數的工具欄通知圖標
- 2. Android工具欄圖標不顯示
- 3. Android:如何顯示此欄並在工具欄中添加圖標?
- 4. 如何在應用程序圖標中顯示通知計數?
- 5. Firefox工具欄上的Firebug顯示錯誤計數圖標
- 6. Xamarin中的工具欄Android沒有顯示圖標
- 7. 在Android的菜單圖標不顯示了使用工具欄
- 8. 新創建的工具欄圖標不顯示在工具欄中
- 9. 如何在android中的通知文本區域顯示圖標
- 10. Android:工具欄未顯示
- 11. 工具欄不顯示android
- 12. 工具欄菜單圖標不顯示
- 13. 工具欄不顯示溢出圖標
- 14. 我如何顯示目標C中的UINavigation欄上的通知計數
- 15. 如何在狀態欄中顯示圖標而不在通知抽屜中顯示通知?
- 16. 按鈕圖標不顯示在Android工具欄上
- 17. 的Android - 工具欄不顯示任何
- 18. 如何顯示在狀態欄上一個通知圖標多個通知
- 19. 如何在通知內容中顯示圖標和文本android
- 20. 如何禁用Android通知欄上的短信通知圖標?
- 21. 如何使用android phonegap在狀態欄顯示通知數
- 22. 在工具欄底部邊緣顯示的工具欄向上箭頭圖標
- 23. 在通知欄中顯示一個窗口服務的圖標?
- 24. 通知圖標不顯示在棒棒糖的狀態欄中
- 25. 返回導航圖標沒有顯示在Android的SearchView中的工具欄中
- 26. GCM通知不會顯示在通知欄中。 Android
- 27. 使用Eddystone信標在Android狀態欄中顯示通知
- 28. 如何在原生反應中顯示圖標上的通知計數?
- 29. 如何在Windows通知欄中顯示彈出通知
- 30. 爲什麼我的菜單圖標不顯示在工具欄中,只顯示空白工具欄
它給錯誤在這條線: - notifCount =(按鈕)count.findViewById(R.id.notif_count); –
什麼是錯誤 – shreyas
空指針異常 –