问题描述
有一个问题!API 22中已弃用getDrawable().因此,如果我使用最小API 16制作应用程序,如何设置图像?
Have a question! getDrawable() is deprecated in API 22. So, if I make an app with the min API 16, how can I set an image?
我看到我可以使用getDrawable(int id,theme),但这是在API 21中添加的,所以我不能使用它.
I saw that I can use getDrawable(int id, theme) , but this was added in API 21, so I can't use it.
我已经尝试过 setImageDrawable(ResourcesCompat.getDrawable(getResources(),R.drawable.drawableName,null))
,但这也不起作用.同样,使用ContextCompat或getApplicationContext()也不起作用.
I've tried setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.drawableName, null))
but this doesn't work either. Also with ContextCompat or getApplicationContext() it doesn't work.
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));
}
这不是该帖子的副本.我从那里尝试了所有方法,但没有一个起作用.
This is not a duplicate of that post. I've tried all the methods from there but none on them worked.
推荐答案
您可以使用
yourImageView.setImageDrawable(ContextCompat.getDrawable(context, /*your drawable like R.drawable.drawableName*/));
这篇关于不推荐使用getDrawable(int id).如何设置图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!