创建自定义通知,安卓

21

我想创建一个类似于这张图片中红色边框通知的通知:

在此输入图片描述

我知道如何创建像这张图片中蓝色边框通知一样的普通通知,但我想在那附近显示一个图标和大约三行信息。我该怎么做?任何建议都将不胜感激。

2个回答

48

在通知中添加RemoteViews。这里是一个示例:

进入图像描述


enter image description here

var remoteViews = new RemoteViews(getPackageName(), R.layout.widget);
var 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);

var notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

// mId allows you to update the notification later on.
notificationManager.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> 

请查看此文章,其中有更多可用的样式。

Android开发者

编辑:

NotificationCompat.Builder是在所有Android版本上创建通知的最简单方法。 您甚至可以使用适用于Android 4.1的可用功能。 如果您的应用程序运行在Android >= 4.1的设备上,则会使用新功能;如果运行在Android < 4.1上,则通知将是旧通知。

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


谢谢, 很好的代码, 但是正如你提到的, 这个适用于API>11. 但是我想让我的应用程序在API>7上运行。 - Reza_Rg
能否处理点击事件或使用自定义视图?就像这样: class MyBTN extends RelativeLayout{...} - Vetalll
@Erick:Sagar,我已经完成了,但在一些设备上无法运行。 - Mehul Joisar
@MehulJoisar 哦,好的...我没有实现那个,但我认为它的默认大小可能是。 - Sagar Maiyad
非常感谢Dhawal,你的博客帮了大忙。 - Sagar Devanga
显示剩余3条评论

8
从Android 4.1开始,可以使用扩展通知来处理这些情况。如果您正在使用Notification.BuilderNotificationCompat.Builder,则需要设置一个普通的Builder和一个独立的Builder用于扩展通知,使用NotificationCompat.InboxStyle或其他样式,并将两者连接起来。

谢谢,有没有相关的链接或教程可供参考? - Reza_Rg
@Reza_Rg: https://github.com/commonsguy/cw-omnibus/tree/master/Notifications/BigNotify - CommonsWare

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接