Android的扩展通知 - 扩展后的标准ContentView和自定义BigContentView

时间:2014-03-05 10:20:31

标签: android notifications remoteview expandable android-4.2-jelly-bean

我在JellyBean中创建了一个自定义RemoteView,如here所述,并将其设置为通知的bigContentView。

notification.bigContentView = customNotifView;

我试图在扩展后将自定义布局放在下面标准contentView;类似于this

问题是扩展后自定义布局会覆盖标准contentView。

有办法吗?

3 个答案:

答案 0 :(得分:4)

我通过为名为layout_base_content_notification.xml的contentView创建自定义布局解决了这个问题,这与Android提供的通知完全相同(手工制作)布局。

RemoteViews collapsedViews = new RemoteViews(c.getPackageName(), R.layout.layout_base_content_notification);
Notification noti = builder.build();
noti.contentView = collapsedViews;

然后我将它包含在一个名为layout_big_content_notification.xml的customLayout中:

<include
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/layout_base_content_notification" />

并将其添加为bigContentView:

RemoteViews expandedViews = new RemoteViews(c.getPackageName(), R.layout.layout_big_content_notification);
noti.bigContentView = expandedViews;

现在,在扩展之后,bigContentView取代了contentView,但它们的标题是相同的。

如果有更好的解决方案,请告诉我。

答案 1 :(得分:2)

很多简单的解决方案看起来像这样

        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.big_notificaiton_layout);
        fillTextViews(profile, contentView);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NotificationChannelHelper.Channel.Channel.getId())
                .setContentIntent(intent)
                .setContentTitle(context.getResources().getString(R.string.profile_name_is_active, profile.getTitle()))
                .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
                .setCustomBigContentView(contentView)
                .setOngoing(true)
                .setSmallIcon(R.drawable.icon);

您必须设置NotificationCompat.DecoratedCustomViewStyle()

答案 2 :(得分:0)

您需要创建自己的RemoteViews,然后表明您希望展开的内容继承自定义RemoteViews

RemoteViews expandedView = new RemoteViews(YOUR CONTEXT.getPackageName(), YOUR CUSTOM LAYOUT);
 Notification notification = mBuilder.build();
 notification.bigContentView = expandedView;

或查看此link.