建立定製化的通知布局
預設情況下,在通知視窗顯示的通知包括標題和訊息文本。這兩項內容使用通過setLatestEventInfo()方法的contentTitle和contentText參數來定義的。但是,你也能夠使用RemoteViews類給通知定義一個定製化的布局。3所示就是一個定製的通知布局的例子。它看上去與預設的通知類似,但是實際上它是用一個定製的XML布局來建立的。
圖3.帶有定製化布局的通知。
要給通知建立自己的布局,就要執行個體化一個RemoteViews對象,用它來填充一個定製的布局檔案,然後把RemoteViews對象傳遞給通知的contentView屬性欄位。
下面用一個例子來更好的理解如何建立定製化的通知:
1. 給通知建立XML布局,如以下在custom_notification.xml檔案中定義的通知布局:
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<ImageViewandroid:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp"/>
<TextViewandroid:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
style="@style/NotificationTitle"/>
<TextViewandroid:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
android:layout_below="@id/title"
style="@style/NotificationText"/>
</RelativeLayout>
我們注意到,兩個TextView元素都包含了style屬性,給定製的通知中的文本使用樣式資源是至關重要的,因為通知的背景色在不同裝置和平台版本中會有所差異。從Android2.3(API層級9)開始,系統給預設的通知布局所使用的文本定義了樣式,這樣你就應該在Android2.3或更高的版本上運行時使用樣式,以便確保文本針對背景是可見的。
例如,要在比Android2.3低的版本上使用標準的文本色,應該使用下列樣式(res/values/styles.xml):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<stylename="NotificationText">
<itemname="android:textColor">?android:attr/textColorPrimary</item>
</style>
<stylename="NotificationTitle">
<itemname="android:textColor">?android:attr/textColorPrimary</item>
<itemname="android:textStyle">bold</item>
</style>
<!-- If you want a slightly different color for some text,
consider using ?android:attr/textColorSecondary -->
</resources>
如果要在Android2.3以上的版本上給通知應用系統預設的顏色,就要使用以下樣式(res/values-v9/styles.xml):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<stylename="NotificationText"parent="android:TextAppearance.StatusBar.EventContent"/>
<stylename="NotificationTitle"parent="android:TextAppearance.StatusBar.EventContent.Title"/>
</resources>
現在,在Android2.3(API層級9)或更高的版本上運行時,在自己定製的View對象中的文本會使用與系統給預設的通知相同的顏色。這是重要的,因為Android的後續版本實際上把通知的背景色改變成深色的。繼承系統的樣式,確保文本會高亮顯示,而且,如果背景是其他的不期望的顏色,那麼文本也要做適當的改變。
2. 現在,在應用程式的代碼中,使用RemoveViews類的方法來定義圖片和文本,然後把RemoteView對象傳遞給通知的contentView屬性欄位,如下例所示:
RemoteViews contentView
=newRemoteViews(getPackageName(),
R.layout.custom_notification_layout);
contentView.setImageViewResource(R.id.image,
R.drawable.notification_image);
contentView.setTextViewText(R.id.title,"Custom
notification");
contentView.setTextViewText(R.id.text,"This
is a custom layout");
notification.contentView
= contentView;
如上例所示,把應用程式的包名和布局資源ID傳遞給RemoteViews類的構造器,然後,使用setImageViewResource()和setTextViewText()方法,給ImageView和TextView對象定義內容。在每個語句中,都要把你設定的適當的View對象的引用ID連同設定給View對象的值一起作為參數傳遞給這兩個方法。最後RemoteViews對象被傳給Notification對象鎮南關的contentView屬性欄位。
3. 因為在使用定製化的View對象時,不需要setLatestEventInfo()方法,就必須用contentIntent欄位給通知定義Intent對象,如下例所示:
Intent notificationIntent = new Intent(this, MyClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
4.
使用通常的方法發送通知:
mNotificationManager.notify(CUSTOM_VIEW_ID, notification);
Remote類還包含了容易把計時器或進度條添加到通知布局中的方法。有關給通知建立定製布局的更多資訊,可參照RemoteViews類。
警告:在建立定製化的通知布局時,必須特別小心,以確保定製化的布局在不同的方向和解析度的裝置中能夠正常的運行。儘管這個建議適用於在Android中建立的所有View物件版面配置,但是在這個情境中尤其重要,因為布局的實際空間非常受限,因此不要讓定製的布局太複雜,並且要在各種配置中做好測試。