动态需要可绘制资源的整数资源ID
Need Integer resource id of drawable resources dynamically
我需要动态设置通知图像图标,其名称动态传递给我的class。
为此,我需要设置 builder.setSmallIcon(int res_id)。我用谷歌搜索,但人们使用了 setDrawableImage() 的例子
我已经看到了这个 post ,并相应地在我的代码中实现了。
//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?post
Of course, this shows error because setSmallIcon() requires integer to be passed to it.
您已经有了想要的整数。您将其命名为 imageResource
。将 imageResource
传递给 setSmallIcon()
。
我需要动态设置通知图像图标,其名称动态传递给我的class。 为此,我需要设置 builder.setSmallIcon(int res_id)。我用谷歌搜索,但人们使用了 setDrawableImage() 的例子 我已经看到了这个 post ,并相应地在我的代码中实现了。
//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?post
Of course, this shows error because setSmallIcon() requires integer to be passed to it.
您已经有了想要的整数。您将其命名为 imageResource
。将 imageResource
传递给 setSmallIcon()
。