java - 如何创建像 android 通知这样的内裤

标签 java android android-studio push-notification android-notifications

如何为 andriod 创建推送通知等短裤。通知包括完整图像和通知底部的文本。

小通知只是文本,但当您展开它时,它会在背景中显示图像,底部显示文本。

有人可以帮我解决这个问题吗?以下是截图

enter image description here

最佳答案

您需要将自定义远程 View 扩充为通知的布局,如下所示:

layout_custom_notification.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image_news"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:src="@mipmap/dine_main_item_bg1" />

    <TextView
        android:id="@+id/tv_news_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="20dp"
        android:layout_toLeftOf="@+id/tv_news_time"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:text="Trump signs order to roll back Obama-era climate policies"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/tv_news_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="20dp"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:text="9:14 AM"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginTop="20dp"
        android:padding="8dp"
        android:text="Share"
        android:textColor="@android:color/white"
        android:textSize="16sp" />

</RelativeLayout>

然后,将此布局设置为通知中的内容 View ,如下所示:

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.layout_custom_notification);
        contentView.setImageViewResource(R.id.image_news, R.drawable.ic_launcher);
        contentView.setTextViewText(R.id.tv_news_time, "9:14 AM");
        contentView.setTextViewText(R.id.tv_news_title, "Trump signs order to roll back Obama-era climate policies");

        Intent notificationIntent = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(icon)
            .setContent(contentView)
            .setContentTitle("Custom Notification")
            .setContentIntent(contentIntent)   
            .setWhen(when);

...
mNotificationManager.notify(1, notificationBuilder.build());

关于java - 如何创建像 android 通知这样的内裤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43085279/

相关文章:

java - 运行 axis2 客户端版本 1.5

java - SSL异常 : SSL peer shut down incorrectly

java - mMap.setMapStyle(GoogleMap.MAP_TYPE_NORMAL) 发生错误; , 为什么?

android - 任务 :app:signReleaseBundle'A failure executing com. android.build.gradle.internal.tasks.Workers$ActionFacade 失败,无法从存储中读取 key

java - Dropbox API 不更新访问级别

java - 如何编写一个 Consumer 来计算它被调用了多少次?

android - 当应用程序被杀死时,FCM 不工作

java - 自定义 ListView 的自定义适配器。空指针异常

android - 进度指示器

android - 如何为 Google Play 发布打包 WatchFace only App