代码之家  ›  专栏  ›  技术社区  ›  Leandros

自定义通知布局在Android 2.3或更低版本上不起作用

  •  18
  • Leandros  · 技术社区  · 13 年前

    我使用 NotificationCompat.Builder 通过android版本显示我的通知,并使用自定义布局进行通知。
    自定义布局在Android 3及更高版本(API Level 11)上运行良好,但在API Level 10或更低版本上不会显示。我在Emulator中的2.3和2.2上测试了它。

    这是我的代码:

        Builder builder = new NotificationCompat.Builder(getApplicationContext());
    
        RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_layout);
        contentView.setImageViewResource(R.id.notImage, R.drawable.stat_icon);
        contentView.setTextViewText(R.id.notTitle, getResources().getString(R.string.streamPlaying));
        contentView.setTextViewText(R.id.notText, StartActivity.streamName + " " + getResources().getString(R.string.playing));
    
        builder
                .setContentTitle(getResources().getString(R.string.streamPlaying))
                .setContentText(StartActivity.streamName + " " + getResources().getString(R.string.playing))
                .setSmallIcon(R.drawable.stat_icon)
                .setContentIntent(pendingIntent)
                .setOngoing(true)
                .setWhen(0)
                .setTicker(StartActivity.streamName + " " + getResources().getString(R.string.playing))
                .setContent(contentView);
    
        not = builder.build();
    

    非常基本。布局文件是正确的,与android.com上的通知教程中的相同,以确保我没有在其中犯错误。;)
    记住:3.0及以上版本运行良好,但2.3及以下版本则不然。

    1 回复  |  直到 13 年前
        1
  •  38
  •   UgglyNoodle    13 年前

    这可能是支持库中的一个错误-请参阅 this issue

    您可能需要通过直接应用contentView来解决此问题:

    not.contentView = contentView;