我需要动态设置通知图像图标,其名称动态传递给我的类。 为此,我需要设置 builder.setSmallIcon(int res_id)。我用谷歌搜索,但人们使用了 setDrawableImage() 的例子 我看过这篇文章,并相应地在我的代码中实现了。
//blah blah lines of code
String uri = "@drawable/a";
int imageResource = con.getResources().getIdentifier(uri,null, con.getPackageName());
Drawable res = con.getResources().getDrawable(imageResource);
builder.setSmallIcon(res);
//blah blah code lines
当然,这会显示错误,因为 setSmallIcon() 需要将整数传递给它。 con 是传递给我的构造函数的上下文。 我看过How to set the image from drawable dynamically in android?发布
最佳答案
Of course, this shows error because setSmallIcon() requires integer to be passed to it.
您已经有了所需的整数。您将其命名为 imageResource
。将 imageResource
传递给 setSmallIcon()
。
关于android - 动态需要可绘制资源的整数资源ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30833381/