Use a custom contentView
on your Notification Builder
To define a custom notification layout, start by instantiating a
RemoteViews object that inflates an XML layout file. Then, instead of
calling methods such as setContentTitle(), call setContent()
. To set
content details in the custom notification, use the methods in
RemoteViews
to set the values of the view's children:
Create an XML layout for the notification in a separate file. You
can use any file name you wish, but you must use the extension .xml
In your app, use RemoteViews
methods to define your notification's icons and text. Put this RemoteViews
object into your
NotificationCompat.Builder
by calling setContent(). Avoid setting a
background Drawable on your RemoteViews object, because your text
color may become unreadable.
custom_push.xml has my custom views R.id.image,R.id.text,R.id.title
<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="10dp" >
<ImageView
android:src="@mipmap/ic_launcher"
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp" />
<TextView
android:textSize="13dp"
android:textColor="#000"
android:text="Testing"
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
/>
<TextView
android:textSize="13dp"
android:textColor="#000"
android:text="Testing is awecome"
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/image"
android:layout_below="@id/title"
/>
</RelativeLayout>
Instantiating a RemoteViews object and set it,
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_push);
contentView.setImageViewResource(R.id.image, R.mipmap.ic_launcher);
contentView.setTextViewText(R.id.title, "Custom notification");
contentView.setTextViewText(R.id.text, "This is a custom layout");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon)
.setContent(contentView);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(1, notification);
check : https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ApplyStyle
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…