2015-06-14 89 views
1

我需要动态设置通知图片图标,其名称是动态传递给我的类的。 为此,我需要设置builder.setSmallIcon(int res_id)。我GOOGLE了它,但人们已经使用setDrawableImage() 的例子我看过这篇文章,并相应地在我的代码中实现。需要动态绘制资源的整型资源ID

//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

当然,这个显示错误,因为setSmallIcon()要求整数传递给它。

您已经拥有所需的整数。你把它命名为imageResource。通过imageResourcesetSmallIcon()