换行符在android studio中不起作用
我试图在.setContentText()中使用换行符发出通知,但这不起作用 我的通知的创建者换行符在android studio中不起作用,android,android-studio,notifications,android-notifications,Android,Android Studio,Notifications,Android Notifications,我试图在.setContentText()中使用换行符发出通知,但这不起作用 我的通知的创建者 builder.setContentTitle("Notification") .setSmallIcon(R.drawable.notif) .setContentText("Example1 "+"\n"+" Example2") .setDefaults(Notifi
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);
在通知中:example1example2
但我需要
Example1
Example2
据我所知,
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);
请参阅:了解更多信息。您可以尝试通过使用strings.xml声明文本来执行换行。转到项目文件夹中的res/values/strings.xml,然后将字符串声明为
<string name="example">Example1 \n Example2</string>
实际上,不仅是这个实例,还应该在String.xml中声明所有字符串值。
有关更多信息,请参阅字符串资源。默认通知布局 使用默认通知布局的多行文本这一想法的主要问题是,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"
/>
因此,如您所见,视图在默认通知布局情况下内部指定了android:singleLine=“true”
,在使用BigTextStyle时内部指定了android:singleLine=“false”
完成任务的最简单方法是使用BigTextStyle,但如果在布局文件中使用自己的TextView并调用:
builder.setCustomContentView(RemoteViews remoteViews)
GDE有一篇很好的文章,解释了如果您选择使用RemoteView,如何使用RemoteView,这篇文章相对简单
祝你好运和快乐 我认为这是行不通的,因为setContentText()仍然会忽略
\n
换行符。
<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>
builder.setCustomContentView(RemoteViews remoteViews)