通知栏设置系统字体颜色

转载自:http://blog.youkuaiyun.com/caroline_wendy/article/details/50115525

Android的Rom版本非常多, 通知栏颜色也各有不同, 如HTC是白底, 红米Note是暗色透明, 也有其他黑底, 灰底等. 统一设置相同的字体颜色, 必然会导致颜色冲突. 那么如何设置字体颜色, 可以正常显示呢?

HTC(5.0), 标题和内容的颜色已经和系统完全相同, 字体大小是适配的. 
HTC5.0

红米Note, 标题和内容的颜色统一使用StatusBar字体的颜色. 
红米Note

通知的逻辑

        RemoteViews views = new RemoteViews(mAppContext.getPackageName(), R.layout.view_notification);
        views.setImageViewResource(R.id.notification_iv_portrait, getSmallIcon());
        views.setTextViewText(R.id.notification_tv_calories, getContentText());
 
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

1. 版本5.0以下

通知的背景默认系统背景, 字体颜色@style/TextAppearance.StatusBar.EventContent.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tool="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:gravity="center_vertical">
    ...
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toEndOf="@+id/notification_iv_portrait"
        android:layout_toRightOf="@+id/notification_iv_portrait"
        android:gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/notification_widget_title"
            android:textAppearance="@style/TextAppearance.StatusBar.EventContent"
            android:textSize="@dimen/d14sp"/>

        <TextView
            android:id="@+id/notification_tv_calories"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="@style/TextAppearance.StatusBar.EventContent"
            android:textSize="@dimen/d20sp"
            tool:text="10步 | 20大卡"/>
    </LinearLayout>
</RelativeLayout>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

TextAppearance.StatusBar.EventContent, 顾名思义StatusBar的事件内容文本样式. 在系统中, 会默认匹配, 如灰色等.

2. 版本5.0及以上

新建layout-v21, 表面5.0版本以后使用.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tool="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    android:gravity="center_vertical">

    <ImageView
        android:id="@+id/notification_iv_portrait"
        android:layout_width="70dp"
        android:layout_height="64dp"
        android:paddingBottom="8dp"
        android:paddingTop="8dp"
        android:scaleType="centerInside"
        tool:src="@drawable/widget_normal"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@+id/notification_iv_portrait"
        android:gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/notification_widget_title"
            android:textAppearance="@android:style/TextAppearance.Material.Notification.Title"
            android:textSize="14sp"/>

        <TextView
            android:id="@+id/notification_tv_calories"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="@android:style/TextAppearance.Material.Notification.Line2"
            android:textSize="20sp"
            tool:text="10步 | 20大卡"/>
    </LinearLayout>
</RelativeLayout>
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

@android:style/TextAppearance.Material.Notification.Title通知栏标题. 
@android:style/TextAppearance.Material.Notification.Line2通知栏内容. 
这样就可以完全匹配系统颜色.

OK, 这个问题已经解决了.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值