How to get the default notification background col

2019-03-18 01:23发布

I'm having an issue with the color of my custom notification background in Lollipop. It's black where it should be white. The textColor is OK:

I'm using

<resources>
    <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />
    <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" />
</resources>

Any idea how to use the device default theme color for custom notification ?

5条回答
闹够了就滚
2楼-- · 2019-03-18 01:51

Your app's targetSdkVersion must be 21.
As Ahmed's answere, add another styles file in values-21 folder.

<?xml version="1.0" encoding="UTF-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
   <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>    
查看更多
萌系小妹纸
3楼-- · 2019-03-18 01:51

For whatever reason, there are new styles in Lollipop. Put this in values-v21/styles.xml:

<resources>
    <style name="NotificationTitle" parent="android:TextAppearance.Material.Notification.Title"></style>
    <style name="NotificationText" parent="android:TextAppearance.Material.Notification"></style>
</resources>
查看更多
▲ chillily
4楼-- · 2019-03-18 01:52

I'm not sure what the theme of the background is but I just set the background of my custom layouts (small view and large view) to a dark colour in the xml. This then matches the pre-Lollipop notifications more closely.

I find it bizarre that the appearance of android:TextAppearance.StatusBar.EventContent.Title doesn't adjust to a dark colour for Lollipop when the standard notification colour is white!

查看更多
冷血范
5楼-- · 2019-03-18 01:56

Create folder values-v21/styles.xml and past the below code,

 <style name="NotificationTitle" parent="@android:style/TextAppearance.StatusBar.EventContent.Title">
    <item name="android:textColor">@android:color/black</item>
    <item name="android:textStyle">normal</item></style>
查看更多
Lonely孤独者°
6楼-- · 2019-03-18 02:02

To solve this, you have 2 options:

1) Target SDK 21, and custom notification will automatically use a white background

2) Copy your existing layout to layout-v21 folder and add android:background="#FFFFFFFF" to its root, like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="#FFFFFFFF" >

...

</LinearLayout>

Works great for me.

查看更多
登录 后发表回答