有一个问题! getDrawable() 在 API 22 中已弃用。那么,如果我使用最小 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));
}
这不是该帖子的重复。我已经尝试了那里的所有方法,但没有一个有效。