美文网首页ViewAndroid知识Android开发
Notification之----自定义样式

Notification之----自定义样式

作者: Hly_Coder | 来源:发表于2016-11-30 10:11 被阅读1157次

概述

Notification之----默认样式中讲了android各个版本中提供的默认样式,现在来讲讲如何在android各个版本中自定义样式。

API Level < 16(JB)

在这个范围的版本里,支持高度等于64dp的自定义view。
不论你的layout写多大的高度,最后只能显示为64dp.

 RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.custom_notification);
 remoteViews.setImageViewBitmap(
                R.id.notification_large_icon,
                BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher));
  remoteViews.setTextViewText(R.id.notification_content, "Content");
  remoteViews.setTextViewText(R.id.notification_title, "title");
  SimpleDateFormat format = new SimpleDateFormat("HH:mm");
  remoteViews.setTextViewText(R.id.notification_time, format.format(new Date()));

  builder.setContent(remoteViews);

API Level >= 16(JB) && API Level < 24(N)

在这个版本里支持了2种高度的自定义view,一种是默认状态64dp的高度,另一种是扩展状态的256dp的高度。
可以给同一条通知设置正常下的显示和扩展后的显示

扩展:指的是下拉通知让其展开的状态。不同rom可能展开方式不一样。

RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.custom_notification);
 remoteViews.setImageViewBitmap(
                R.id.notification_large_icon,
                BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher));
remoteViews.setTextViewText(R.id.notification_content, "Content");
remoteViews.setTextViewText(R.id.notification_title, "title");
SimpleDateFormat format = new SimpleDateFormat("HH:mm");
remoteViews.setTextViewText(R.id.notification_time, format.format(new Date()));

builder.setContent(remoteViews);

RemoteViews remoteViewBig = new RemoteViews(mContext.getPackageName(), R.layout.custom_notification_big);
notification = builder.build();
notification.bigContentView = remoteViewBig;

使用notification的bigContentView 变量来设置扩展后的显示内容。

API Level >= 24(N)

在N上,虽然任然可以使用notification的bigContentView 变量来设置扩展后的内容,但是该属性已被标记为@Deprecated,notification.bigContentView = remoteViewBig. 我们可以使用其他推荐的方式来设置bigContentView。
v4包,version24版本中NotificationCompat.Builder新增了一个方法setCustomBigContentView

RemoteViews remoteViewBig = new RemoteViews(mContext.getPackageName(), R.layout.custom_notification_big);
builder.setCustomBigContentView(remoteViewBig);

这样就可以设置bigContentView了。
Notification之----默认样式可以知道,在版本N上,系统默认的通知栏样式和高度(变高)已经改变,具体高度还不是很清楚,但是应该比之前的默认高度要大(>64dp)。所以写demo的时候发现,如果设置了bigcontentview,则只会显示bigcontentview,不知道是不是N上做的新改动,笔者后续研究后会给出结果,如果有读者知道答案也请告知~

最后

github demo下载

相关阅读

Notification之----Android5.0实现原理(二)
Notification之----Android5.0实现原理(一)
Notification之---NotificationListenerService5.0实现原理
Notification之----默认样式
Notification之----任务栈

相关文章

  • Notification详解

    Notification详解 Notification的使用步骤 自定义Notification样式 自定义Not...

  • Notification之----自定义样式

    概述 在Notification之----默认样式中讲了android各个版本中提供的默认样式,现在来讲讲如何在a...

  • Android之Notification自定义样式

    Notification 自定义的时候高度一般不能定制(小米,vivo,oppo定制后高度固定,华为和魅族手机部分...

  • Notification之----默认样式

    概述 随着android版本的不断升级,google在4.1, 5.0,7.0上不断的推出了几款通知样式来满足不同...

  • 自定义系统样式Notification

    起因 很久之前接到一个需求,需要在通知栏常驻一个notification,title和subtitle都比较短,在...

  • Android 全局管理 Activity 栈

    前言 最近在写 Notification 的 Demo,梳理 Notification 的各种样式以及点击 Not...

  • 自定义Notification样式遇到的坑

    顺带一提用系统样式自带的是必须设置三个属性 不然会报错:比如 想要使用自定义的Notification的时候当然是...

  • 自定义进度条之样式篇

    自定义进度条基础篇之样式表 样式(style) 1.1 系统是默认样式有style="@style/Widget....

  • Notification通知

    Notification notification是在系统的通知栏中呈现多样式持久性消息的类。 在通知栏显示 消息...

  • 4.1 CSS书写规范

    全局CSS样式 CSS样式可细分为3类:自定义样式、重新定义HTML样式、链接状态样式。 自定义样式为设计师自定义...

网友评论

  • a1cc38198f51:bigContentView 设置无效. 使用链接中的demo也一样. 手机: Le Max2 API:23 .6.0.1
    a1cc38198f51:@Hly_Coder 有的有的. 用 bigPictureStyle是可以展示大图的. 不是魅族的要兼容嘛, 打算使用 bigContentView来试试, 结果显示不了 Extend区域
    Hly_Coder:你是否有双指下滑通知栏?

本文标题:Notification之----自定义样式

本文链接:https://www.haomeiwen.com/subject/vtkzpttx.html