Это фрагмент кода.Как отличить BitmapDrawable к LayerDrawable в android
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the main; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
MenuItem item = menu.findItem(R.id.action_notifications);
LayerDrawable icon = (LayerDrawable) item.getIcon();
// Update LayerDrawable's BadgeDrawable
Utils2.setBadgeCount(this, icon, mNotificationsCount);
return true;
}
Это дает ошибка в строке
LayerDrawable icon = (LayerDrawable) item.getIcon();
BitmapDrawable не может быть приведен к android.graphics.drawable.LayerDrawable
Как бросить BitmapDrawable к слою LayerDrawable?
Редактировать: добавление функции setBadgeCount.
public static void setBadgeCount(Context context, LayerDrawable icon, int count) {
BadgeDrawable badge;
// Reuse drawable if possible
Drawable reuse = icon.findDrawableByLayerId(R.id.ic_badge);
if (reuse != null && reuse instanceof BadgeDrawable) {
badge = (BadgeDrawable) reuse;
} else {
badge = new BadgeDrawable(context);
}
badge.setCount(count);
icon.mutate();
icon.setDrawableByLayerId(R.id.ic_badge, badge);
}
'LayerDrawable' не является« BitmapDrawable ». Вы не можете отдать его другому. – Blackbelt
@Blackbelt Спасибо за ответ, Тогда какое лучшее решение для этой проблемы. Я добавил setBadgeCount() в сообщение. –
Что такое 'BadgeDrawable'? В чем проблема? – Blackbelt