需要动态地绘制可绘制资源的整数资源ID

时间:2015-06-14 19:07:10

标签: android dynamic android-resources

我需要动态设置通知图像图标,其名称会动态传递给我的类。 为此,我需要设置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?帖子

1 个答案:

答案 0 :(得分:1)

  

当然,这显示错误,因为setSmallIcon()需要传递整数。

您已经拥有了所需的整数。你将它命名为imageResource。将imageResource传递给setSmallIcon()

相关问题