在Android應用開發中,常常會使用到狀態欄通知(Status Notification),例如新浪微博、網易新聞等提供的推送消息,軟件後臺更新時進度的顯示等等,以下圖所示: android
看了網上不少關於Notification的博客文章,發現幾乎沒有一個能將自定義狀態欄通知徹底實現正確的,所以,本文就來講說實現自定義狀態欄通知常常被忽略的一些知識點。 app
1) 使用Notification最多見的場景 佈局
運行在後臺的Service當須要和用戶交互時,因爲它不能直接啓動一個Activity,所以,Service必須使用Notification來間接的啓動Activity(當用戶點擊Notification時跳轉)。 字體
2) 自定義佈局文件支持的控件類型 spa
Notification的自定義佈局是RemoteViews,所以,它僅支持FrameLayout、LinearLayout、RelativeLayout三種佈局控件,同時支持AnalogClock、Chronometer、Button、ImageButton、ImageView、ProgressBar、TextView、ViewFlipper、ListView、GridView、StackView和AdapterViewFlipper這些UI控件。對於其餘不支持的控件,使用時將會拋出ClassNotFoundException異常。 xml
3) Notification支持的Intent類型(都是PendingIntent類的實例) 事件
contentIntent:在通知窗口區域,Notification被單擊時的響應事件由該intent觸發; ip
deleteIntent:在通知窗口區域,當用戶點擊所有清除按鈕時,響應該清除事件的Intent; utf-8
fullScreenIntent:響應緊急狀態的全屏事件(例如來電事件),也就是說通知來的時候,跳過在通知區域點擊通知這一步,直接執行fullScreenIntent表明的事件。 開發
上面三種PendingIntent能夠拉起Activity、Service和BroadcastReceiver,如圖所示:
4) 狀態欄通知字體的設置
不一樣的手機,不一樣的Android平臺版本,狀態欄通知窗口的背景顏色可能千差萬別,例如Android2.3以前的版本通知窗口默認背景是白色的,Android4.0以後的版本通知窗口背景默認是黑色的,這就須要在設置Notification的字體時加以區別,不然,很容易致使通知的字體顏色和背景色同樣,從而看不到字體部分,市面上不少app就存在這個問題。以下圖所示,華爲智匯雲和百度音樂這兩款應用就明顯存在這個問題。
![](http://static.javashuo.com/static/loading.gif)
從Android2.3(API level 9)開始,系統爲默認通知欄佈局的字體定義了樣式以下:
"android:TextAppearance.StatusBar.EventContent"
"android:TextAppearance.StatusBar.EventContent.Title"
所以,在2.3以後的版本中咱們自定義佈局文件中的字體直接應用這個樣式就能夠。對於2.3以前的版本,因爲背景色是白色的,所以,咱們使用以下系統預約義樣式指定字體的顏色:
?android:attr/textColorPrimary
所以,在res的values目錄下定義styles.xml文件以下:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
-
- <style name="NotificationText">
- <item name="android:textColor">?android:attr/textColorPrimary</item>
- </style>
-
- <style name="NotificationTitle">
- <item name="android:textColor">?android:attr/textColorPrimary</item>
- <item name="android:textStyle">bold</item>
- </style>
-
- </resources>
在res的values-v9目錄下定義styles.xml文件以下:
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
-
- <style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />
-
- <style name="NotificationTitle" parent="android:TextAppearance.StatusBar.EventContent.Title" />
-
- </resources>
自定義通知佈局文件使用styles文件以下:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/layout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- tools:ignore="ContentDescription" >
-
- <ImageView
- android:id="@+id/image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_centerVertical="true"
- android:layout_marginLeft="5.0dp"
- android:layout_marginRight="10.0dp" />
-
- <RelativeLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerVertical="true"
- android:layout_toRightOf="@id/image" >
-
- <TextView
- android:id="@+id/title"
- style="@style/NotificationTitle"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
-
- <TextView
- android:id="@+id/text"
- style="@style/NotificationText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/title" />
- </RelativeLayout>
-
- </RelativeLayout>