自定义通知布局不适用于 Android 2.3 或更低版本 [英] Custom notification layout don't work on Android 2.3 or lower

查看:28
本文介绍了自定义通知布局不适用于 Android 2.3 或更低版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use the NotificationCompat.Builder to display my notification through android versions and use a custom layout for the notification.
The custom layout works fine on Android 3 and up (API Level 11), but don't show up on API Level 10 or lower. I tested it on 2.3 and 2.2 in the Emulator.

这是我的代码:

    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 及更低版本上运行良好.

really basic. The layout file is correct, its the same as in the notification tutorial on android.com, to make sure I didn't make a mistake in there. ;)
Remember: Working fine on 3.0 and up, but not at 2.3 and lower.

推荐答案

这可能是支持库中的错误 - 请参阅 这个问题.

This may be a bug in the support library - see this issue.

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

You may have to work around it by applying the contentView directly:

not.contentView = contentView;

这篇关于自定义通知布局不适用于 Android 2.3 或更低版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆