Adding actions to a notification in jellybean ignores the custom layout set with setContent

这一生的挚爱 提交于 2019-12-10 17:34:11

问题


I'm creating a notification with a custom layout using setContent, and it works fine. But when I'm adding actions to the notification with addAction(), my custom layout is being ignored and it shows up the android's default notification layout.

When I shrink the notification (using the two fingers gesture) my custom layout shows, so it seems the "expanded" form uses a different layout which I cannot set.

Screenshots (With the actions, and then after swiping two fingers up to shrink it)

As you can see, it shows up empty (=default layout) when the actions are shown.

The code:

RemoteViews remoteView = new RemoteViews(context.getPackageName(), R.layout.notification_status);
Builder builder = new Notification.Builder(context);
builder.setSmallIcon(icon)
    .setTicker(tickerText)
    .setWhen(when)
    .setContent(remoteView)
    .setOngoing(true)
    .setContentIntent(contentIntent)
    .setPriority(Notification.PRIORITY_HIGH)
    .addAction(R.drawable.ic_remove, "Action 1", cancelPendingIntent)
    .addAction(R.drawable.ic_stat_notify_gray_official, "Action 2", cancelPendingIntent)
    .setContentIntent(contentIntent);
Notification statusNotification = builder.build();
return statusNotification;

I tried to find somewhere to control the notification-with-actions layout, with no luck. Any help?


回答1:


Had exactly the same issue! Instead of calling

builder.setContent(remoteView)

Which replaces the normal content, you should do it like this:

Notification statusNotification = builder.build();
statusNotification.bigContentView = remoteViews;

Which will set your layout to the bigContentView, which becomes visible after expanding!

NOTE: Doing this will prevent your action buttons from showing up. See In Android (on JB), how can I add an action to a custom rich notification? for how to get around this



来源:https://stackoverflow.com/questions/12875090/adding-actions-to-a-notification-in-jellybean-ignores-the-custom-layout-set-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!