有一个问题!在API 22中,getDrawable()已经被弃用。因此,如果我使用最小的API 16制作一个应用程序,我该如何设置图片?
我看到可以使用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));
}
这不是那篇帖子的副本。我已经尝试了那里所有的方法,但它们都没有起作用。
getDrawable()
方法已被弃用,但它仍然可以正常工作,所以可能是你的代码其他地方出了问题。 - Harlan