目錄
1.示例:
2.View層級:
屬性:
type(id) | Width | Height | Margin(dp) | Padding(dp) | Gravity | Layout_gravity | Visibility | others | ||||||
top | bottom | start | end | top | bottom | start | end | |||||||
NotificationHeaderView(notification_header) | wrap_content | 48dp | horizontal | |||||||||||
CachingIconView(icon) | 18dp | 18dp | 3dp | |||||||||||
TextView(app_name_text) | wrap_content | wrap_content | 3dp | 2dp | singleLine | |||||||||
TextView(header_text_divider) | wrap_content | wrap_content | 2dp | 2dp | gone | |||||||||
TextView(header_text) | wrap_content | wrap_content | 2dp | 2dp | gone | singleLine | ||||||||
TextView(time_divider) | wrap_content | wrap_content | 2dp | 2dp | gone | singleLine | ||||||||
DateTimeView(time) | wrap_content | wrap_content | 2dp | 2dp | center | gone | singleLine | |||||||
Chronometer(chronometer) | wrap_content | wrap_content | 2dp | 4dp | gone | singleLine | ||||||||
NotificationExpandButton(expand_button) | wrap_content | wrap_content | 1dp | gone | ||||||||||
ImageView(profile_badge) | 12dp | 12dp | 4dp | 1dp | center | gone | fitCenter |
3.使用方法:
- CachingIconView(icon):
調用setSmallIcon方法設置
- TextView(app_name_text)
系統默認取應用的app_name來顯示。
允許系統應用修改:
需要申請此權限:
<!-- @SystemApi Allows an application to replace the app name displayed alongside notifications in the N-release and later.
@hide <p>Not for use by third-party applications.</p> -->
<permission android:name="android.permission.SUBSTITUTE_NOTIFICATION_APP_NAME"
android:protectionLevel="signature|privileged" />
修改方法:
builder.getExtras().putString(Notification.EXTRA_SUBSTITUTE_APP_NAME, "name");
- TextView(header_text_divider):
這個不允許修改,系統會控制隱藏與顯示,並且隨文字的顏色變化而變化
- TextView(header_text):
修改方法:
setContentTitle方法:
setBigContentTitle方法:
衝突管理:
- 只調用setContentTitle方法 顯示此方法設置的Title
- 只調用setBigContentTitle方法 顯示此方法設置的Title
- 同時調用上面2個方法 顯示setBigContentTitle方法設置的Title
結論:優先使用setBigContentTilte方法設置的Title;若此方法沒有設置,則使用setContentTitle方法設置的Title;若都沒有設置,則顯示title的View隱藏
- TextView(time_divider):
這個不允許修改,系統會控制隱藏與顯示,並且隨文字的顏色變化而變化
- DateTimeView(time):
功能:顯示通知已經發出了多場時間 例如:剛剛、1分鐘前、2分鐘前、1小時前等等
顯示的配置:
setShowWhen設置爲true(默認值爲false)
setUsersChronometer設置爲false(默認值爲false)
setWhen不能設置爲0(默認值爲System.currentTimeMillis()獲取的毫秒值)
- Chronometer(chronometer):
功能:顯示精確計時:用戶可以根據需要配置時間,並且可以選擇是否爲倒計時
顯示配置:
setUsersChronometer設置爲true(默認值爲false)
setWhen不能設置爲0(默認值爲System.currentTimeMillis()獲取的毫秒值)
setChronometerCountDown true(倒計時) false(正計時) 默認值爲false
setShowWhen方法無論設置爲true還是fasle DateTimeView都會被隱藏
注:
1.使用精確計時時調用setWhen方法需要以System.currentTimeMillis()爲基準,也就是說setWhen(System.currentTimeMillis() + 偏移時長)。
2.精確計時的最大單位爲小時,最小單位爲秒。若要顯示幾天或幾月或幾年,則換算成小時顯示在冒號的前面。
- NotificationExpandButton(expand_button):
功能:展開或隱藏通知的內容(通知內容非常多或者有多條通知時顯示)
系統根據通知信息來控制顯示與隱藏,當顯示時,用戶可以通過點擊來控制內容全部顯示或顯示部分內容
- ImageView(profile_badge):
功能:多用戶特性中用來提示用戶此通知是普通用戶的APP所發出來的而不是管理用戶(設備的擁有者,唯一)
此圖標由系統根據userId來控制顯示與隱藏,不提供API接口控制
4.自定義Header:
方法:setCustomHeadsUpContentView
/**
* Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
*
* This will override the heads-up layout that would otherwise be constructed by this
* Builder object.
*/
public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
mN.headsUpContentView = contentView;
return this;
}
當系統提供的Layout不能滿足需求時,可以使用此方法來設計你需要的Header,建議使用之前參考系統的Layout設計來完善你設計的Layout.
5.總結:
- 通讀此篇文章可以全面的瞭解系統通知Header的所有功能。
- 可以通過此篇文章學會通知Header的相關View的設置方法
- 此篇文章是學習不同通知Style的基礎。