2017-08-27 79 views

回答

1

您可以使用您的通知生成器

定製contentView要定義自定義通知佈局,通過實例化一個膨脹XML佈局文件 RemoteViews對象開始。然後,而不是調用諸如setContentTitle()之類的方法,調用setContent()。要在自定義通知中設置 內容詳細信息,請使用 RemoteViews中的方法設置視圖子項的值:

在單獨的文件中爲通知創建XML佈局。您可以使用 使用您希望的任何文件名,但必須使用擴展名.xml在 您的應用程序中,使用RemoteViews方法來定義通知的圖標 和文本。通過調用setContent()將此RemoteViews對象放入您的NotificationCompat.Builder中。避免你的RemoteViews對象上設置一個 背景繪製對象,因爲你的文字 顏色可能變得不可讀。

而且代碼如下:

RemoteViews mycontentView = new RemoteViews(getPackageName(), R.layout.notification); 
mycontentView.setImageViewResource(R.id.myimage, R.mipmap.ic_launcher); 
mycontentView.setTextViewText(R.id.mytitle, "Custom Notification"); 

NotificationCompat.Builder myBuilder = new NotificationCompat.Builder(this) 
.setSmallIcon(R.drawable.icon) 
.setContent(mycontentView); 

Notification myNotification = myBuilder.build(); 
myNotification.flags |= Notification.FLAG_AUTO_CANCEL; 
myNotification.defaults |= Notification.DEFAULT_SOUND; 
myNotification.defaults |= Notification.DEFAULT_VIBRATE; 
myNotificationManager.notify(1, myNotification); 

其中R.layout.notification是您的自定義佈局文件

和佈局文件低於

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/layout" 
    android:layout_width="fill_parent" 
    android:layout_height="64dp" 
    android:padding="12dp" > 
    <ImageView 
     android:src="@mipmap/ic_launcher" 
     android:id="@+id/myimage" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_marginRight="10dp" /> 
    <TextView 
     android:textSize="12dp" 
     android:textColor="#000" 
     android:text="Testing" 
     android:id="@+id/mytitle" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/myimage" 
     /> 

</RelativeLayout> 

我希望它能幫助。