简体   繁体   English

在Android通知中显示多行标题

[英]Showing multiline titles in Android notifications

I have gone through the following tutorials 我已经完成了以下教程

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

  2. https://developer.android.com/training/notify-user/build-notification.html 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 . 您没有为LinearLayout设置任何方向。

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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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