转载请注明出处Android 4.1--大布局Notification
今天发现了一个谷歌4.1以上才有的的新型Notification,完成度也算比较高,自己记录一下
首先我们先看看区别所在:
1,普通的Notification
2,大布局的Notification
这是android4.1以后才增加的,与普通Notification的区别主要就是其显示内容可以扩展,并且只有在所有的Notification的最上面时才会显示大布局,其他情况下显示小布局,也可以用手指将其扩展为大布局。
大布局也可分为三种类型:
InboxStyle收件箱风格:显示多行文字
BigTextStyle大文字风格:显示一个大的文字块
BigPictureStyle大图片风格:详情区域包含一个256dp高度的位图
上面这些都来自于网上的文章,接下来上代码
public void onCreate() {
manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
textStyle = new BigTextStyle();
builder = new NotificationCompat.Builder(this);
super.onCreate();
}
先实例化一个NotificationManager,一个大文字风格的Notification,一个NotificationCompat。
textStyle.setBigContentTitle(mName);
textStyle.bigText(mSinger);
builder.setSmallIcon(R.drawable.notif_icon)
.setLargeIcon(result)
.setTicker("播放")
.setContentTitle(mName)
.setContentIntent(intentApp())
.setContentText(mSinger)
.setStyle(textStyle)
.setSound(null)//声音提示
.setAutoCancel(true)
.setDefaults(0)
.setOngoing(true)//常驻不消失
manager.notify(2, builder.build());
以上就是一个普通的 大文字风格的Notification,看下效果。
等等有点奇怪啊。。为什么展开之后会多了几个按钮,这是考虑到一些音乐App可能会这样设计,这是怎么做到的呢,其实也没有几句代码。
.addAction(R.drawable.pre_video_button_nomal, null,
preMuscic())
.addAction(R.drawable.play_button, null, playMuscic())
.addAction(R.drawable.next_video_button_nomal, null,
nextMuscic()).build();
NotificationCompat.Builder.addAction就可以了。。
Android笔记--Notification 这篇文章讲解的更加详细。。有需要的朋友可以去看看
记录一下,不积跬步无以至千里