2015-06-14 86 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()