android - 换行符在 Notifications android studio 中不起作用

标签 android android-studio notifications android-notifications

我试图在 .setContentText() 中发出一个带有换行符的通知,但这不起作用。

我的通知的构建者

builder.setContentTitle("Notification")               
                .setSmallIcon(R.drawable.notif)
                .setContentText("Example1 "+"\n"+" Example2")  
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true)
                .setContentIntent(contentIntent)
                .setTicker("testing it")
                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                .setPriority(Notification.PRIORITY_HIGH);

在通知中:Example1 Example2

但我需要

 Example1
 Example2

最佳答案

据我所知,setContentText() 中的换行符被忽略,因为它被压缩了。你可以试试BigTextStyle,例如:

builder.setContentTitle("Notification")               
                .setSmallIcon(R.drawable.notif)
                .setStyle(new NotificationCompat.BigTextStyle()
                     .bigText("Example1\nExample2")
                )
                .setDefaults(Notification.DEFAULT_ALL)
                .setAutoCancel(true)
                .setContentIntent(contentIntent)
                .setTicker("testing it")
                .setVibrate(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400})
                .setPriority(Notification.PRIORITY_HIGH);

参见:https://developer.android.com/reference/android/app/Notification.BigTextStyle获取更多信息。

关于android - 换行符在 Notifications android studio 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51143105/

相关文章:

android - 无法初始化 FirebaseApp - Flutter

android - 工作管理器会唤醒应用程序类吗?它是在应用程序进程还是其他进程上运行?

android - okhttp 获取失败响应

android - Kotlin - 将 UTC 时间转换为本地时间

android - 错误:未知主机 'jcenter.bintray.com: Name or service not known'。您可能需要在Gradle中调整代理设置

java - bundle.getParcelableArrayList() 返回 null

Android 到 PC 通知平台

ios - 如何知道用户在推送通知中点击了 "Don' t 允许”

android - 如何使用状态栏文本(无图标)创建通知

java - 访问 DialogPreference 中的小部件状态