回答
我認爲這是可能的:
<ImageView
android:id="@+id/counterBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/unread_background" /> <!-- your icon -->
<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" />
,然後用它爲圖標,background
。還有,你需要刪除/禁用默認的圖標了。
你可能想看一看:
Remove App Icon from Navigation Drawer ActionBar Does not work
Remove the navigation drawer icon and use the app icon to open
https://stackoverflow.com/a/29160904/4409113
此外,在[android-sdk]/platforms/android-22/data/res
應該有那個圖標,你只需要找到和使用它爲你的目的(例如,添加imageview並添加它像background
)
這裏是一步一步的功能:
添加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,這種佈局將用作通知圖標佈局
<?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;
}
你可以把下面的函數(從計算器所)的活動中來更新計數器:
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();
}
}
});
}
你只是想說在你的下面的代碼: '如果(count == 0) txtViewCount.setVisibility(View.GONE);' –
是如果計數== 0然後View.Gone。 –
那麼,如何將菜單項對齊到工具欄左側? (如上圖) – Mucahit
- 1. shinydashboard徽章menuitem
- 2. 如何在android 5.0工具欄中添加徽章/計數通知在android 5.0棒棒糖中使徽章/計數的工具欄通知圖標
- 3. 如何在啓動圖標上添加未讀徽章數?
- 4. 如何在UIBarbutton項上添加徽章?
- 5. OS X上的SWT:如何將徽章添加到停靠欄圖標
- 6. 如何在自定義工具欄上添加圖標?
- 7. 如何添加TabBar徽章?
- 8. 是否有Windows API將徽章添加到任務欄圖標?
- 9. 如何添加自定義圖標stimulsoft工具,工具欄
- 10. 在Windows/OSX/Linux上向Python中的圖標添加徽章
- 11. 在android的應用程序圖標上添加通知徽章
- 12. 在離子3中的圖標上添加徽章
- 13. 如何將圖標(圖像,徽標..)添加到狀態欄
- 14. 在導航欄上的切換圖標旁邊添加徽標
- 15. 在標籤頁中添加徽章
- 16. 如何在導航欄中添加徽章,即xamarin.forms圖標上的數字標籤?
- 17. AppCompatActivity在工具欄中設置徽標
- 18. android在Expandablelistview上添加徽章
- 19. 在viewpager上添加通知徽章數
- 20. 工具欄上的抽屜徽章通知切換 - Android
- 21. 將圖標添加到Extjs工具欄
- 22. Facebook如何在Android的應用圖標上添加徽章號碼?
- 23. 如何在導航欄上添加徽標?
- 24. 如何在Android應用程序欄中添加徽標圖像?
- 25. 如何添加工具欄
- 26. 在主屏幕上添加的網站圖標上顯示標準徽章
- 27. 使用工具欄的大小在Android工具欄上設置徽標
- 28. 如何在Drupal 8工具欄中添加公司圖標
- 29. Notepad ++:如何在記事本++工具欄中添加新圖標
- 30. Android工具欄MenuItem LowerCase
有很多的例子已經在這裏就如何使徽章。你到底有什麼問題? –