【发布时间】:2016-07-22 11:32:08
【问题描述】:
有一个问题! getDrawable() 在 API 22 中已弃用。所以,如果我使用 min 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()已被弃用,但它现在仍然可以工作,所以你的代码可能在其他地方有问题
标签: android