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。