android - getDrawable(int id) 已弃用。如何设置图像?

标签 android

有一个问题! 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));


            }

这不是该帖子的副本。我已经尝试了那里的所有方法,但都没有奏效。

最佳答案

你可以使用

yourImageView.setImageDrawable(ContextCompat.getDrawable(context, /*your drawable like R.drawable.drawableName*/));

关于android - getDrawable(int id) 已弃用。如何设置图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38525390/

相关文章:

java - 当 Android Activity 失去焦点时,如何隐藏或显示布局元素?

android - 在 Android 手机上查看 ionic 应用程序时无法查看图像

android - 如何在 Flutter 中打印 String 函数的结果?

android - Samsung S4 (Android 4.4.2) 上 WebView 中的 HTML5 视频问题

java - 从共享首选项将数组传递到 ArrayAdapter。安卓

android - 如何在 Android 对话框中显示版权符号?

Android SQLite - 主键 - 插入表

java - 无法实例化类型 PopupMenu.OnMenuItemClickListener

android - setColorFilter 更改按钮颜色无效

android - 如果 Hook onFocusChangeListener,TextInputEditText 的奇怪行为