Android 那些年踩过的坑

1. Notification bar icon turns white in Android 5 Lollipop

http://stackoverflow.com/questions/28387602/notification-bar-icon-turns-white-in-android-5-lollipop

I have an app showing custom notifications. The problem is that when running in Android 5 the small icon in the Notification bar shows white. How can I fix this?

The accepted answer is not (entirely) correct. Sure, it makes notification icons show in color, but does so with a BIG drawback - by setting the target SDK to lower than Android Lollipop!

If you solve your white icon problem by setting your target SDK to 20, as suggested, your app will not target Android Lollipop, which means that you cannot use Lollipop-specific features.

Have a look at http://developer.android.com/design/style/iconography.html, and you'll see that the white style is how notifications are meant to be displayed in Android Lollipop.

In Lollipop, Google also suggest that you use a color that will be displayed behind the (white) notification icon - https://developer.android.com/about/versions/android-5.0-changes.html

So, I think that a better solution is to add a silhouette icon to the app and use it if the device is running Android Lollipop.

For instance:

Notification notification = new Notification.Builder(context)
            .setAutoCancel(true)
            .setContentTitle("My notification")
            .setContentText("Look, white in Lollipop, else color!")
            .setSmallIcon(getNotificationIcon())
            .build();

    return notification;

And, in the getNotificationIcon method:

private int getNotificationIcon() {
    boolean whiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
    return whiteIcon ? R.drawable.icon_silhouette : R.drawable.ic_launcher;
}


posted @ 2015-10-09 14:07  lotusJade  阅读(182)  评论(0编辑  收藏  举报