有疑問! getDrawable()在API 22中已棄用。因此,如果我使用min API 16創建應用程序,如何設置圖像?getDrawable(int id)已棄用。我如何設置圖像?
我看到我可以使用getDrawable(int id,theme),但是這是在API 21中添加的,所以我不能使用它。我試過setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.drawableName, null))
但是這也行不通。還有ContextCompat或getApplicationContext()它不起作用。
private ImageView weatherIcon;
weatherIcon=(ImageView)findViewById(R.id.weatherIcon);
if(weatherReportList.size()>0){
DailyWeatherReport report=weatherReportList.get(0);
switch (report.getWeather()){
case DailyWeatherReport.WEATHER_TYPE_CLOUDS:
weatherIcon.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.cloudy, null));
weatherIconMini.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.cloudy, null));
case DailyWeatherReport.WEATHER_TYPE_RAIN:
weatherIcon.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.rainy));
weatherIconMini.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.rainy, null));
default:
weatherIcon.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.sunny, null));
weatherIconMini.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.sunny, null));
}
這不是該帖子的副本。我已經嘗試過所有的方法,但沒有一個能夠工作。
檢查這裏:http://stackoverflow.com/a/29041466/3626214 – Aspicas
可能重複的[Android的getResources()。getDrawable()不贊成API 22](http://stackoverflow.com/questions/29041027/android -getresources-getdrawable-deprecated-api-22) –
儘管'getDrawable()'不推薦使用,但它現在仍然可以正常工作,所以可能在其他地方的代碼中有錯誤 – Harlan