Notification视觉风格
Notification有两种视觉风格,一种是标准视图(Normal view)、一种是大视图(Big view)。标准视图在Android中各版本是通用的,但是对于大视图而言,仅支持Android4.1+的版本。
从官方文档了解到,一个标准视图显示的大小要保持在64dp高,宽度为屏幕标准。标准视图的通知主体内容有一下几个:

Notification有两种视觉风格,一种是标准视图(Normal view)、一种是大视图(Big view)。标准视图在Android中各版本是通用的,但是对于大视图而言,仅支持Android4.1+的版本。
从官方文档了解到,一个标准视图显示的大小要保持在64dp高,宽度为屏幕标准。标准视图的通知主体内容有一下几个:

通知标题。大图标。通知内容。通知消息。小图标。通知的时间,一般为系统时间,也可以使用setWhen()设置。
下面通过一个示例,模仿上面效果的通知。
btnNotification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap btm = BitmapFactory.decodeResource(getResources(),
R.drawable.msg);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
MainActivity.this).setSmallIcon(R.drawable.msg)
.setContentTitle("5 new message")
.setContentText("twain@android.com");
mBuilder.setTicker("New message");//第一次提示消息的时候显示在通知栏上
mBuilder.setNumber(12);
mBuilder.setLargeIcon(btm);
mBuilder.setAutoCancel(true);//自己维护通知的消失
//构建一个Intent
Intent resultIntent = new Intent(MainActivity.this,
ResultActivity.class);
//封装一个Intent
PendingIntent resultPendingIntent = PendingIntent.getActivity(
MainActivity.this, 0, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// 设置通知主题的意图
mBuilder.setContentIntent(resultPendingIntent);
//获取通知管理器对象
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
}); 复制代码
显示效果:
而对于大视图(Big View)而言,它的细节区域只能显示256dp高度的内容,并且只对Android4.1+之后的设备才支持,它比标准视图不一样的地方,均需要使用setStyle()方法设定,它大致的效果如下:
setStyle()传递一个NotificationCompat.Style对象,它是一个抽象类,Android为我们提供了三个实现类,用于显示不同的场景。分别是:
NotificationCompat.BigPictureStyle, 在细节部分显示一个256dp高度的位图。NotificationCompat.BigTextStyle,在细节部分显示一个大的文本块。NotificationCompat.InboxStyle,在细节部分显示一段行文本。
如果仅仅显示一个图片,使用BigPictureStyle是最方便的;如果需要显示一个富文本信息,则可以使用BigTextStyle;如果仅仅用于显示一个文本的信息,那么使用InboxStyle即可。后面会以一个示例来展示InboxStyle的使用,模仿上面图片的显示。
实现代码:
1234567891011121314151617181920212223242526272829303132333435btnBigViewNotification.setOnClickListener(
new
View.OnClickListener() {
@Override
public
void
onClick(View v) {
Bitmap btm = BitmapFactory.decodeResource(getResources(),
R.drawable.msg);
Intent intent =
new
Intent(MainActivity.
this
,
ResultActivity.
class
);
PendingIntent pendingIntent = PendingIntent.getActivity(
MainActivity.
this
,
0
, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
Notification noti =
new
NotificationCompat.Builder(
MainActivity.
this
)
.setSmallIcon(R.drawable.msg)
.setLargeIcon(btm)
.setNumber(
13
)
.setContentIntent(pendingIntent)
.setStyle(
new
NotificationCompat.InboxStyle()
.addLine(
"M.Twain (Google+) Haiku is more than a cert..."
)
.addLine(
"M.Twain Reminder"
)
.addLine(
"M.Twain Lunch?"
)
.addLine(
"M.Twain Revised Specs"
)
.addLine(
"M.Twain "
)
.addLine(
"Google Play Celebrate 25 billion apps with Goo.."
)
.addLine(
"Stack Exchange StackOverflow weekly Newsl..."
)
.setBigContentTitle(
"6 new message"
)
.setSummaryText(
"mtwain@android.com"
))
.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(
0
, noti);
}
});