gpt4 book ai didi

显示空白文本和标题的 Android 自定义推送通知

转载 作者:太空狗 更新时间:2023-10-29 13:55:16 24 4
gpt4 key购买 nike

我正在尝试设置自定义通知布局

 RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
contentView.setImageViewResource(R.id.image_customNote,R.mipmap.ic_notification);
contentView.setTextViewText(R.id.title_customNote,title);
contentView.setTextViewText(R.id.text_customNote, messageBody);


Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(true)
//.setContentText(title)
// .setContentText(messageBody)
.setTicker(title)
.setSound(defaultSoundUri)
/* .setStyle(new NotificationCompat.BigTextStyle().bigText(title))
.setStyle(new NotificationCompat.BigTextStyle().bigText(messageBody))*/
.setCustomContentView(contentView)
.setCustomBigContentView(contentView)
.setContentIntent(pendingIntent)
.setSmallIcon(getNotificationIcon()).setColor(ContextCompat.getColor(this, R.color.colorPrimary));
// .addAction(R.drawable.ic_launcher, "Previous", pendingIntent)
// .setColor(ContextCompat.getColor(this, R.color.colorPrimary));

notificationManager.notify(new Random().nextInt(), notificationBuilder.build());

这是我正在尝试的代码,

这是我的xml文件

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

<ImageView
android:id="@+id/image_customNote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp" />

<TextView
android:id="@+id/title_customNote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/black"
android:layout_toRightOf="@id/image_customNote"

/>

<TextView
android:id="@+id/text_customNote"
android:textColor="@android:color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title_customNote"
android:layout_toRightOf="@id/image_customNote" />
</RelativeLayout>

当我收到通知时,自定义 View 未设置且通知 View 完全空白...请帮忙

最佳答案

问题出在您的 xml 中。

  1. 您没有“title”的 ID。您需要将第二个 textView 设置为 android:layout_below="@id/title_customNote"
  2. 删除样式 - 并为 TextView 设置文本颜色 - 文本已设置,但颜色是透明的。

这个 xml 对我有用:

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

<ImageView
android:id="@+id/image_customNote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="10dp" />

<TextView
android:id="@+id/title_customNote"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/holo_blue_dark"
android:layout_toRightOf="@id/image_customNote" />

<TextView
android:id="@+id/text_customNote"
android:textColor="@android:color/holo_blue_dark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/title_customNote"
android:layout_toRightOf="@id/image_customNote" />
</RelativeLayout>

enter image description here

关于显示空白文本和标题的 Android 自定义推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40260942/

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