gpt4 book ai didi

android - 创建自定义通知,android

转载 作者:IT老高 更新时间:2023-10-28 23:08:45 26 4
gpt4 key购买 nike

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

enter image description here

我知道如何创建普通通知,例如此图像中的蓝色边框通知,但我想在该图标附近显示一个图标和大约 3 行信息。我怎样才能做到这一点?任何建议将不胜感激。

最佳答案

在通知中添加 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>

检查 article还有更多样式可供选择

Android Developer

编辑:

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/

关于android - 创建自定义通知,android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16168553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com