在Android通知中显示多行标题

时间:2019-04-26 02:00:00

标签: android android-notifications

我已经阅读了以下教程

  1. https://developer.android.com/training/notify-user/expanded.html

  2. https://developer.android.com/training/notify-user/build-notification.html

我要显示多行通知,如下图所示。Prototype layout

但是我无法看到多行文本,如下图所示。第一行是标题,第二行是正文。

Actual Notification Title

这是我构建通知构建器的代码。

// 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>

1 个答案:

答案 0 :(得分:0)

您没有为LinearLayout设置任何方向。

android:orientation="false"替换为     android:orientation="vertical"

enter image description here