创build自定义通知,android

我想创build一个通知,就像这张图片中红色边界的通知一样:

在这里输入图像描述

我知道如何创build正常的通知,如图中蓝色的边界通知,但是我想在旁边显示一个图标和大约3行信息。 我怎样才能做到这一点? 任何build议将不胜感激。

在通知中添加RemoteView这里是示例

在这里输入图像描述

RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.widget); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder( this).setSmallIcon(R.drawable.ic_launcher).setContent( remoteViews); // Creates an explicit intent for an Activity in your app Intent resultIntent = new Intent(this, test.class); // The stack builder object will contain an artificial back stack for // the // started Activity. // This ensures that navigating backward from the Activity leads out of // your application to the Home screen. TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); // Adds the back stack for the Intent (but not the Intent itself) stackBuilder.addParentStack(test.class); // Adds the Intent that starts the Activity to the top of the stack stackBuilder.addNextIntent(resultIntent); PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT); remoteViews.setOnClickPendingIntent(R.id.button1, resultPendingIntent); NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); // mId allows you to update the notification later on. mNotificationManager.notify(100, mBuilder.build()); 

widget.xml

 <?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="horizontal" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="DJ notification" android:textAppearance="?android:attr/textAppearanceMedium" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Close Me" /> </LinearLayout> 

检查这篇文章有更多的风格avaialbe

Android开发人员

编辑:

NotificationCompat.Builder是在所有Android版本上创build通知最简单的方法。 您甚至可以使用Android 4.1提供的function。 如果您的应用程序在Android> = 4.1的设备上运行,那么将会使用新function,如果在Android <4.1上运行,通知将是一个简单的旧通知。

<11 API使用http://www.framentos.com/en/android-tutorial/2012/02/20/how-to-create-a-custom-notification-on-android/

Android 4.1以上版本提供扩展通知来处理这些情况。 如果您使用的是Notification.BuilderNotificationCompat.Builder ,则可以使用NotificationCompat.InboxStyle或其他样式之一设置扩展通知的普通Builder和单独的Builder ,然后连接两者。