【问题标题】:Add programmatically created layout in remoteview for Notification在远程视图中为通知添加以编程方式创建的布局
【发布时间】:2014-01-17 08:51:45
【问题描述】:

我正在开发一个应用程序,在该应用程序中我不必为notification 使用自定义布局。相反,我必须使用programmaticallyRemoteViews 创建的布局。这是我正在为布局做的事情:

Notification notify = new Notification.Builder(context)
            .setContentTitle("Notification")
            .setLargeIcon(
                    decodeBase64(NotificationImages.notification_ic_stat_notify))
            .build();

    LinearLayout linLayout = new LinearLayout(context);

    @SuppressWarnings("deprecation")
    Drawable bg = new BitmapDrawable(
            decodeBase64(NotificationImages.search_bg));

    linLayout.setOrientation(LinearLayout.HORIZONTAL);
    linLayout.setBackground(bg);

    LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.MATCH_PARENT);

    linLayout.setLayoutParams(llp);

    ImageView icon = new ImageView(context);

    @SuppressWarnings("deprecation")
    Drawable icon_bg = new BitmapDrawable(
            decodeBase64(NotificationImages.notification_logo));

    icon.setBackground(icon_bg);

    LinearLayout.LayoutParams image_lp = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    image_lp.setMargins(20, 0, 0, 0);
    image_lp.gravity = Gravity.CENTER_VERTICAL;

    icon.setLayoutParams(image_lp);

    linLayout.addView(icon);

忽略base64 解码的使用,这是我出于某种随机目的而做的。 所以,现在我想要将这个layout 添加到RemoteViews 以在notification panel 上显示它。 像这样的:

RemoteViews contentView = new RemoteViews(PACKAGE_NAME,
            R.layout.persistent_notification_layout);

如何,我可以从以编程方式创建的布局中替换 layout.persistent_notification_layout 吗? 任何形式的帮助将不胜感激。

【问题讨论】:

  • 这里您的 LinearLayout 和 ImageView 计数是固定的。仅出现 1 个或更多
  • 是的,它是固定的,只有一个Linear Layout
  • 那么 ImageView 呢?也是固定的吗?
  • 我想做同样的事情。我正在远程视图中添加以编程方式创建的布局,但它不起作用...

标签: android android-layout android-remoteview


【解决方案1】:

让我们退后一步,为什么您“必须不使用自定义布局进行通知”,我不清楚这是什么意思。然后你说“相反,我必须为 RemoteViews 使用以编程方式创建的布局。” - 为什么,你在做什么,这些都是真的?也许如果您能更深入地了解您想要完成的工作,我就能理解您的目标并帮助您实现这一目标。

【讨论】:

  • 嘿 Travis,实际上我正在为 android 项目构建一个库 jar 文件,它会在 onCreate() 上显示通知,因为你知道 jar 文件不能包含 drawableslayout 资产他们。所以,为了克服这个问题,我正在考虑使用程序为Notification 实现remoteview。你知道我为什么想以编程方式使用而不是使用布局吗?
【解决方案2】:

我已经解决了这个问题,方法是不从库文件中提供布局,而是从包含库的应用程序中提供。我已经添加了这个功能来创建通知布局。

public void searchBar(Context context, String packageName, int layout,
        String tickerText, int icon) {

您可以看到,对于正在使用此库的应用程序而言,这里的所有内容都是动态的。布局和所有其他内容都可以从应用程序本身提供。

【讨论】:

  • 这不是问题的真正答案。更改您提供的问题或解决方案。提前致谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多