创建自定义通知,Android

我想创建一个类似于该图像中带有红色边框的通知的通知:

http://img3.mukewang.com/5d9fe998000149e703000512.jpg

我知道如何创建正常的通知,例如此图像中带有蓝色边框的通知,但是我想显示一个图标,以及附近的三行信息。我怎样才能做到这一点?任何建议将不胜感激。


不负相思意
浏览 476回答 2
2回答

拉风的咖菲猫

添加RemoteViews通知。这是一个示例:var remoteViews = new RemoteViews(getPackageName(), R.layout.widget);var mBuilder = new NotificationCompat.Builder(this)&nbsp; &nbsp; .setSmallIcon(R.drawable.ic_launcher)&nbsp; &nbsp; .setContent(remoteViews);// Creates an explicit intent for an Activity in your appIntent 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 stackstackBuilder.addNextIntent(resultIntent);PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);remoteViews.setOnClickPendingIntent(R.id.button1, resultPendingIntent);var notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);// mId allows you to update the notification later on.notificationManager.notify(100, mBuilder.build());&nbsp;widget.xml<?xml version="1.0" encoding="UTF-8"?><LinearLayout&nbsp; &nbsp; xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; android:layout_width="fill_parent"&nbsp; &nbsp; android:layout_height="fill_parent"&nbsp; &nbsp; android:gravity="center"&nbsp; &nbsp; android:orientation="horizontal">&nbsp; &nbsp; <TextView&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/textView1"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:gravity="center"&nbsp; &nbsp; &nbsp; &nbsp; android:text="DJ notification"&nbsp; &nbsp; &nbsp; &nbsp; android:textAppearance="?android:attr/textAppearanceMedium" />&nbsp; &nbsp; <Button&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/button1"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:text="Close Me" /></LinearLayout>&nbsp;检查这篇文章还有更多的样式可供选择Android开发人员编辑:NotificationCompat.Builder是在所有Android版本上创建通知的最简单方法。您甚至可以使用Android 4.1可用的功能。如果您的应用程序在Android> = 4.1的设备上运行,则将使用新功能;如果在Android <4.1的设备上运行,则通知将是简单的旧通知。对于<11 API,请使用http://www.framentos.com/zh-CN/android-tutorial/2012/02/20/how-to-create-a-custom-notification-on-android/

缥缈止盈

从Android 4.1开始,可以使用扩展的通知来处理这些情况。如果您使用Notification.Builder或NotificationCompat.Builder,则可以使用或其他样式之一为展开的通知设置普通样式和Builder单独样式,然后将两者连接起来。BuilderNotificationCompat.InboxStyle
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android