1.示例:
此篇文章主要分析红框内的内容。
2.View层级:
View属性:
type(id) | Width | Height | Margin(dp) | Padding(dp) | Gravity | Layout_gravity | Visibility | others | ||||||
top | bottom | start | end | top | bottom | start | end | |||||||
FrameLayout(status_bar_latest_event_content) | match_parent | wrap_content | ||||||||||||
LinearLayout(notification_main_column) | match_parent | wrap_content | 37.5dp | 16dp | 16dp | 16dp | top | vertical | ||||||
LinearLayout(line1) | match_parent | wrap_content | horizontal | |||||||||||
TextView(title) | wrap_content | wrap_content | singleLine | |||||||||||
TextView(text_line_1) | match_parent | wrap_content | 16dp | end|bottom | singleLine | |||||||||
ImageFloatingTextView(text) | match_parent | wrap_content | 0.5dp | top | top | singleLine | ||||||||
ProgressBar(progress) | match_parent | 15dp | 15dp | 16dp | 16dp | bottom | gone | |||||||
ImageView(right_icon) | 40dp | 40dp | 36dp | 16dp | top|end | top|end |
NotificationHeaderView(notification_header) 参考:https://blog.csdn.net/zhao5214319/article/details/98038482
3.View详解:
- LinearLayout(notification_main_column):
功能:布局文件,规定标题和内容的位置的,应用中无法修改
- LinearLayout(line1):
功能:布局文件,规定标题位置,应用中无法修改
- TextView(title):
功能:通知的Title显示
方法:setContentTitle
- TextView(text_line_1):
功能:一般用户显示进度条的最大进度值,Title此时显示为当期进度
也可显示进度条当前进度
方法:同时调用setProgress和setContentText
- ImageFloatingTextView(text):
功能:显示通知内容
方法:setContentText
- ProgressBar(progress):
功能:显示进度;一般用来显示下载进度
方法:setProgress
- ImageView(right_icon):
功能:大图标提示
方法:setLargeIcon
4.示例代码:
@TargetApi(26)
private void createNotificationChannel() {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID ,CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
mNotificationManager.createNotificationChannel(channel);
}
@TargetApi(26)
private Notification buildNotification() {
Notification.Builder builder = new Notification.Builder(this, CHANNEL_ID);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.bitmap_small));
builder.setContentTitle("Notification");
builder.setContentText("I'm is Text");
builder.setSubText("SubText");
builder.setProgress(100, 50, false);
builder.setShowWhen(true);
builder.setUsesChronometer(true);
return builder.build();
}
mNotificationManager.notify(NOTIFICATION_ID, buildNotification());
5.总结:
- Normal Style通知是使用最多的通知,了解其View的层级以及设置方法可以帮助研发更快的设计需要的通知。
- 不调用setStyle方法时,通知默认使用的Style是Noraml。