【问题标题】:Android's Expanded Notifications - Both Standard ContentView and custom BigContentView after expansionAndroid的扩展通知-扩展后的标准ContentView和自定义BigContentView
【发布时间】:2014-03-05 10:20:31
【问题描述】:

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

notification.bigContentView = customNotifView;

我正在尝试将自定义布局放置在扩展后的标准 contentView 下方;类似于this

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

有没有办法做到这一点?

【问题讨论】:

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


    【解决方案1】:

    我通过为名为 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;
    

    然后我将它包含在一个 customLayout 中,称为layout_big_content_notification.xml

    <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,但它们的 header 是一样的。

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

    【讨论】:

    【解决方案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()

    【讨论】:

    • "Added in API 24" 这个问题是从 2014 年开始的,所以没有人提到它。感谢您的跟进!
    【解决方案3】:

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

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

    或查看link.

    【讨论】:

    • 谢谢@Harshit,但本教程没有提供任何自定义 bigContentView 的示例。我真的需要这样做,因为没有一个默认样式符合我的要求。
    • 这正是我正在做的事情(事实上,您提供的链接与我在问题中提出的相同)。问题是expandedViews 替换通知的默认contentView,而我想在扩展之后追加它......我的意思是,我需要contentView和同时在屏幕上显示 bigContentView。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多