问题描述
|
我需要创建一个带有较长文本的通知,这可能吗?默认情况下不是,但是您可以使用自定义布局,这就是我所做的。现在,我可以显示多行,但是正如您所看到的,文本仍然是折断的/没有完全显示? ):有人可以告诉我我做错了吗/通知大小是否有固定限制?如果查看屏幕截图,您会注意到,仍然还有很多空间...感谢您提供任何提示!
顺便说一句,这是用于自定义布局的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
,用于在通知区域中增加视图:
<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>
, 我的理解是,Android的通知系统每个通知的高度有限,以避免单个通知填满屏幕。
在您链接的页面上:
注意:使用自定义通知布局时,请格外小心,以确保自定义布局可在不同的设备方向和分辨率下使用。尽管此建议适用于所有View布局,但对于通知尤为重要,因为通知抽屉中的空间非常有限。不要使您的自定义布局过于复杂,请确保在各种配置中对其进行测试。
但是,您可以显示多个通知,“粘性”通知或在通知内滚动文本。
有关可以对通知执行的操作的更多信息,请参见:
通知和
通知生成器
, Android提供大视图可扩展通知,它们支持3种样式,bigpicture样式,inbox样式,big text样式(256 dp),但仅来自Android版本,然后是Jelly Bean。对于较低版本,我们没有任何大字体样式通知。
, 这是对我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));