Lollipop notification background color

别来无恙 提交于 2019-12-10 11:24:50

问题


I can't seem to make my notification use default notification background color: it remains gray where it should be white. At the same time, notification colors are appropriate in kitkat.

I have implemented suggestions in this question

There doesn't seem to be any effect on what I implement. The values-v21 simply acts as if it weren't there. Naturally, my target sdk is 21. I just can't seem to find the reason for this.

The notification is displayed from service using StartForeground. I have also tried NotificationManager.notify(), but it makes no difference.

Also, the notification is gray even if I only leave the wrapper RelativeLayout in the xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

values-v21\styles.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="NotificationText" parent="android:TextAppearance.Material.Notification" />
  <style name="NotificationTitle" parent="android:TextAppearance.Material.Notification.Title" />
  <style name="NotificationTime" parent="android:TextAppearance.Material.Notification.Time" />
</resources>

Though these styles aren't used anywhere else (like in my app theme declaration). They are just defined (in values-v21 and values-v9).


回答1:


I can't seem to make my notification use default notification background color: it remains gray where it should be white.

No. At least, not on Lollipop.

The darkish-gray background (#ff424242) will be used (in place of white) if you set Notification.MediaStyle on your Notification.Builder instance:

From BaseStatusBar:

protected void applyColorsAndBackgrounds(StatusBarNotification sbn,
        NotificationData.Entry entry) {

    if (entry.expanded.getId() != com.android.internal.R.id.status_bar_latest_event_content) {
        ....
        ....
    } else {
        // Using platform templates
        final int color = sbn.getNotification().color;
        if (isMediaNotification(entry)) {
            entry.row.setTintColor(color == Notification.COLOR_DEFAULT
                    ? mContext.getResources().getColor(
                            R.color.notification_material_background_media_default_color)
                    : color);
        }
    }
 ....
}

isMediaNotification checks if you are using Notification.MediaStyle. The following takes place if this check comes through:

if (No color was set on Notification.Builder instance using `setColor()`) {
    // R.color.notification_material_background_media_default_color 
    // is used as background
} else {
    // The color you set is used instead.
}

R.color.notification_material_background_media_default_color:

<color name="notification_material_background_media_default_color">#ff424242</color>

Perhaps you can confirm this by taking a screenshot of your notification and reading the argb value of its background. If the issue is introduced by MediaStyle, the color value will be as above. Even simpler approach would be to use setColor(int) on your Builder instance and see if that makes a difference.

This could very well be a Lollipop-specific thing since MediaStyle isn't available on < 21.




回答2:


<color name="notification_background_light">#fffafafa</color>

matched for me in Android 6 (Marshmallow).

See: Guide lines "Grey 50".




回答3:


I had the same problem - my notification color was black on Lollipop while all other notifications were of system color (white). I could fix it by changing targetSdkVersion to 23



来源:https://stackoverflow.com/questions/28363247/lollipop-notification-background-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!