简体   繁体   中英

Showing multiline titles in Android notifications

I have gone through the following tutorials

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

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

I want to show notifications with multiple line as shown in the layout below. 原型布局

But I am not able to see the multiline text as shown in the image below. The first line is the title, while the second line is the body.

实际通知标题

Here is my code to build the notification builder.

// 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();

Layout file

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

You didn't set any orientation for LinearLayout .

Replace android:orientation="false" with android:orientation="vertical"

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM