Android學習16

Notification視覺風格

  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("[email protected]");
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的使用,模仿上面圖片的顯示。

  實現代碼:
?
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
btnBigViewNotification.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("[email protected]"))
.build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, noti);
}
});
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章