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

标签 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?发布

最佳答案

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/

相关文章:

c# - 从 xml 和 c# 创建具有层次结构的动态对象

android - 如何访问我放在 res 文件夹中的原始资源?

c# - 当从 C# 中的动态获取属性时,如何检查枚举属性?

java - Robolectric Resources$NotFoundException 找不到库 (aar) 资源

Android 资源阿拉伯语本地化

java - 在另一个类(class)中使用什么上下文共享偏好?

java - AsyncTask 非常慢

java - 通过静态方法访问单例的字段

android - 我想像 iphone HUD 进度条一样显示自定义对话框

C - realloc() 函数 - 证明它会损坏数据