[英]Android multiline notifications / notifications with longer text
我需要创建一个包含更长文本的通知,这可能吗? 默认情况下不是,但您可以使用自定义布局 ,这就是我所做的。 现在我可以显示多行,但正如您所看到的,文本仍然被破坏/未完全显示? ):有人可以告诉我我做错了什么/如果通知的大小有固定的限制吗? 如果你看一下屏幕截图,你会注意到,还剩下很多空间......感谢任何提示!
BTW这里是用于自定义布局的XML,基于http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomNotification
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="3dp"
>
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#000"
/>
</LinearLayout>
对于Jelly Bean及更高版本,您可以使用可扩展通知。 最简单的方法是使用NotificationCompat.BigTextStyle进行通知。
像这样:
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle();
bigTextStyle.setBigContentTitle(getString(R.string.title));
bigTextStyle.bigText(getString(R.string.long_explanation));
mBuilder.setStyle(bigTextStyle);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(message))
.setContentText(message)
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.setContentIntent(contentIntent)
.setAutoCancel(true);
mNotificationManager.notify(requestID, mBuilder.build());
曾经有人试过https://developer.android.com/guide/topics/ui/notifiers/notifications.html
通知视图的高度限制为65sp
。 这是实现细节,没有记录, 并且已在Android 4.1中进行了更改以支持可扩展通知 。 所以不要依赖这个特定的值,而是依赖于视图的高度有限的事实。
以下是status_bar_latest_event.xml
,用于在通知区域中status_bar_latest_event.xml
视图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="65sp"
android:orientation="vertical"
>
<com.android.server.status.LatestItemView android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="64sp"
android:background="@drawable/status_bar_item_background"
android:focusable="true"
android:clickable="true"
android:paddingRight="6sp"
>
</com.android.server.status.LatestItemView>
<View
android:layout_width="match_parent"
android:layout_height="1sp"
android:background="@drawable/divider_horizontal_bright"
/>
</LinearLayout>
这对我来说对5.0有用。它很好地包裹了长线。 它还允许您提供一个字符串数组,这些字符串将以新行分隔显示。
String[] multilineDescription = new String[] { "line 1", "another very long line that will get wrapped." };
NotificationCompat.Builder builder = new NotificationCompat.Builder(appContext)
.setSmallIcon(smallIcon)
.setContentTitle(title)
.setContentText("Pull down for more information");
String description;
if (null == multilineDescription || multilineDescription.length == 0) {
description = "No more information.";
} else {
description = multilineDescription[0];
for (int i=1; i<multilineDescription.length; i++) {
description += System.getProperty("line.separator");
description += multilineDescription[i];
}
}
builder.setStyle(new NotificationCompat.BigTextStyle().bigText(description));
Android提供了一个大视图可扩展通知,他们支持3种风格,bigpicture风格,收件箱风格,大文本风格(256 dp),但只有Android版本大于果冻豆。 对于较低版本,我们没有任何大文本样式通知。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.