系統(tǒng)默認的通知風(fēng)格
可以看到盟蚣,Android通知欄默認是標(biāo)題顯示一行肋联,內(nèi)容顯示一行,對于一行顯示不完的刁俭,用省略號代替橄仍。
private void showNotification(Context context, int id, String title, String text) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle(title);
builder.setContentText(text);
builder.setAutoCancel(true);
builder.setOnlyAlertOnce(true);
// 需要VIBRATE權(quán)限
builder.setDefaults(Notification.DEFAULT_VIBRATE);
builder.setPriority(Notification.PRIORITY_DEFAULT);
// Creates an explicit intent for an Activity in your app
//自定義打開的界面
Intent resultIntent = new Intent(context, FlashPageActivity.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(id, builder.build());
}
這是常見的通知欄的樣式,不再贅述牍戚。
在不同的手機上侮繁,只設(shè)置builder.setSmallIcon(R.drawable.ic_launcher)表現(xiàn)可能不一致,在華為P9上不會顯示大的icon如孝,只會顯示小的icon宪哩。但是在魅族上卻顯示了,顯示的應(yīng)該是應(yīng)用的默認圖標(biāo)第晰。
所以建議還是設(shè)置
builder.setLargeIcon(BitmapFactory.decodeResource(context.getResources()
,R.drawable.ic_launcher));
內(nèi)容顯示多行的處理
首先想到的就是自定義布局來實現(xiàn)通知欄锁孟,自己控制怎么顯示,當(dāng)然能實現(xiàn)內(nèi)容顯示幾行茁瘦,但是這樣又會帶來自定義布局怎么和系統(tǒng)默認通知欄布局的統(tǒng)一品抽,比如間距,文字大小甜熔,圖片大小等等圆恤。很難在各種國產(chǎn)手機系統(tǒng)上統(tǒng)一。(個人觀點)
還有一種方式腔稀,就是Android 4.1中官方推出的Notification.BigTextStyle盆昙,從名稱也可以看出是專門為長文本推出的一種通知欄風(fēng)格,實測通知欄會顯示最大256dp高度焊虏,這個范圍內(nèi)的文本都能顯示出來淡喜,超過再用省略號代替。
需要注意的是這個方法只在4.1及以上才能生效诵闭,4.1以下不要調(diào)用
// Big views were introduced in Android 4.1, and they're not supported on older devices.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(text)
.setBigContentTitle(title));
}
顯示大圖
與上面的顯示多行文本類似悦污,也是在Android4.1中官方推出的Notification.BigPictureStyle。
使用方式也相同
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
builder.setStyle(new Notification.BigPictureStyle()
.bigPicture(aBigBitmap));
}
自定義通知欄
- 自定義通知欄就是自己定義通知欄的布局陷猫,但是在不同手機上表現(xiàn)可能不太一致,比如小米通知欄左右都會又個pandding值币叹,你如果給布局的icon設(shè)置pandding或者margin,就會變得很難看模狭。但是在華為上卻好了很多颈抚,除了文字默認都是白色,會看不到以外嚼鹉。
- 如果你設(shè)置的高度大于通知欄的默認高度贩汉,需要設(shè)置bigContentView模式,否則锚赤,會內(nèi)容會顯示不全匹舞,通過
builder.setCustomBigContentView(remoteViews);
這種方式,之前的notification.bigContentView = remoteViews;
這種方式已經(jīng)被廢棄了线脚。當(dāng)然赐稽,需要判斷在Android4.1及以上才能使用。記住所有的大通知欄模式都是在Android4.1及以上才引入的 - 至于為什么還要設(shè)置
builder.setSmallIcon(R.mipmap.ic_icon);
builder.setContent(remoteViews);
builder.setContentIntent(contentIntent);
是因為在用戶還沒有劃出通知欄的時候需要提醒用戶收到通知浑侥,所以需要顯示smallIcon姊舵。
- 源碼如下
private void showRemoteView() {
Intent intent = new Intent(this, MaxBoxActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews remoteViews = new RemoteViews(getPackageName(),
R.layout.layout_custom_notifycation);
remoteViews.setImageViewResource(R.id.iv_icon, R.mipmap.ic_icon);
remoteViews.setTextViewText(R.id.tv_title, "我是一個有內(nèi)涵的程序猿");
remoteViews.setTextViewText(R.id.tv_description,
"你懂我的,作為新時代的程序員寓落,要什么都會括丁,上刀山下火海,背磚搬磚都要會伶选,不然哈哈哈哈哈哈哈哈哈你就慘了");
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_icon);
builder.setContent(remoteViews);
builder.setContentIntent(contentIntent);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
builder.setCustomBigContentView(remoteViews);
}
NotificationManager notificationManager = (NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE);
notificationManager.notify(111, builder.build());
}
-
效果圖
可以看到在小米上大圖模式是不支持的史飞,華為手機是支持的,并且左右兩邊都有一個系統(tǒng)設(shè)置的padding或者margin值仰税,很難看构资,華為左邊也有一個padding值,所以建議自定義布局的icon不要設(shè)置左右的margin值肖卧。
- 還有上面說過默認的如果不給自定義布局的TextView設(shè)置任何顏色的話蚯窥,在華為(Android7.0)上是白色的字體,也就看不見塞帐。那怎么解決這個問題呢?
目前來說有兩種方式:自己設(shè)置通知欄的背景色和字體顏色/字體顏色使用系統(tǒng)通知欄的顏色。
自己設(shè)置背景色不建議巍沙,因為在小米手機上明顯看到自定義的通知欄的左右都有margin值葵姥,那就會很難看。
使用系統(tǒng)通知欄風(fēng)格的方式,新建一個layout-v21存放5.0以上的布局句携,對TextView設(shè)置文字風(fēng)格
// 5.0及以上
android:textAppearance="@android:style/TextAppearance.Material.Notification.Info"
// 5.0以下
android:textAppearance="@style/TextAppearance.StatusBar.EventContent.Info"