换行符在Notifications Android Studio中不起作用

时间:2018-07-02 20:09:29

标签: 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

3 个答案:

答案 0 :(得分:3)

据我所知,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

答案 1 :(得分:2)

默认通知布局

这种具有默认通知布局的多行文本的思想的主要问题是,在TextView的基础源代码中具有以下xml:

<com.android.internal.widget.ImageFloatingTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:layout_marginTop="@dimen/notification_text_margin_top"
    android:ellipsize="marquee"
    android:fadingEdge="horizontal"
    android:gravity="top"
    <!-- Reason why a new line character won't work -->
    android:singleLine="true"
    android:textAlignment="viewStart"
    android:textAppearance="@style/TextAppearance.Material.Notification"
    />

大字体样式

如您所见,默认视图已硬编码为一行。如此说来,如果您使用上面答案中提到的BigTextStyle,那么它将起作用,因为基础xml使用以下布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/status_bar_latest_event_content"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:tag="bigText"
    >
    <include layout="@layout/notification_template_header" />

    <LinearLayout
            android:id="@+id/notification_action_list_margin_target"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="top"
            android:layout_marginTop="@dimen/notification_content_margin_top"
            android:layout_marginBottom="@dimen/notification_action_list_height"
            android:clipToPadding="false"
            android:orientation="vertical">

        <LinearLayout
            android:id="@+id/notification_main_column"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:paddingStart="@dimen/notification_content_margin_start"
            android:paddingEnd="@dimen/notification_content_margin_end"
            android:clipToPadding="false"
            android:minHeight="@dimen/notification_min_content_height"
            android:orientation="vertical"
            >
            <include layout="@layout/notification_template_part_line1" />
            <include layout="@layout/notification_template_progress"
                android:layout_width="match_parent"
                android:layout_height="@dimen/notification_progress_bar_height"
                android:layout_marginTop="@dimen/notification_progress_margin_top"
                android:layout_marginBottom="6dp"/>
            <com.android.internal.widget.ImageFloatingTextView
               android:id="@+id/big_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/notification_text_margin_top"
              android:paddingBottom="@dimen/notification_content_margin_bottom"
             android:textAppearance="@style/TextAppearance.Material.Notification"
              <!-- Reason why it works here -->                    
                android:singleLine="false"
                android:gravity="top"
                android:visibility="gone"
                android:textAlignment="viewStart"
                />
        </LinearLayout>

        <ViewStub android:layout="@layout/notification_material_reply_text"
                android:id="@+id/notification_material_reply_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
    </LinearLayout>
    <include layout="@layout/notification_material_action_list" />
    <include layout="@layout/notification_template_right_icon" />
</FrameLayout>

因此您可以看到,在默认的通知布局情况下,视图内部指定了android:singleLine="true",在使用BigTextStyle时,视图指定了android:singleLine="false"

完成任务的最简单方法是使用BigTextStyle,但如果在布局文件中使用自己的TextView并调用,则可以对Layout进行更多控制。

builder.setCustomContentView(RemoteViews remoteViews)

GDE here上有一篇很好的文章,解释了如何使用RemoteViews(如果您愿意的话),这是相对简单的。

祝你好运,编码愉快!

答案 2 :(得分:-1)

您可以尝试通过使用strings.xml声明文本来执行换行符。转到项目文件夹中的res / values / strings.xml,然后将String声明为

 <string name="example">Example1 \n Example2</string>

然后转到您的活动,并在文本旁边写上 R.string.example,或者您可以使用String example = getString(R.string.example)获取String并在setContentText中使用String变量。

.setContentText(R.string.example) or .setContentText(example)

实际上,不仅是此实例,还应该在string.xml中声明所有String值。 有关更多知识,请参见字符串资源documentation