我已经阅读了以下教程
https://developer.android.com/training/notify-user/expanded.html
https://developer.android.com/training/notify-user/build-notification.html
但是我无法看到多行文本,如下图所示。第一行是标题,第二行是正文。
这是我构建通知构建器的代码。
// Helper function to create NotificationChannels and a builder for the channel id.
final NotificationCompat.Builder builder = makeNotificationBuilder();
if (builder == null)
return null;
final RemoteViews collapsedView = new RemoteViews(context, R.layout.notification_template);
builder.setContent(contentView);
// Trying to set the text
NotificationCompat.BigTextStyle style = new NotificationCompat.BigTextStyle();
builder.setStyle(style);
if (notification.getTitle() != null)
builder.setContentTitle(notification.getTitle());
style.setBigContentTitle(notification.getTitle());
if (notification.getBody() != null)
builder.setContentText(notification.getBody());
style.bigText(notification.getBody());
. . . . . Code to set the intent.. It works
if (expandedView != null)
builder.setCustomBigContentView(expandedView);
builder.setAutoCancel(true);
return builder.build();
布局文件
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="false"
android:background="@drawable/notification_material_bg">
<TextView
android:id="@+id/notification_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:fadingEdge="horizontal"
android:singleLine="false"
android:maxLines="2"
android:textAppearance="@style/NotificationTitle"
tools:ignore="Deprecated"
/>
<TextView
android:id="@+id/notification_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom"
android:layout_weight="1"
android:ellipsize="end"
android:fadingEdge="horizontal"
android:textAppearance="@style/NotificationText"
tools:ignore="Deprecated"
/>
</LinearLayout>