Android getDrawable() 已弃用/如何使用 Android getDrawable()

标签 android android-view android-drawable

谁能告诉我使用 getDrawable() 的正确方法,我记得上次使用它时只用一个参数就可以了,但现在我收到了 this method is deprecated 错误,有人吗?下面是我尝试使用的 fragment

 private void toggleUi() {
    ImageView imageView = (ImageView) findViewById(R.id.silent_icon);
    Drawable silentImage;

    if(silent) {
        silentImage = getResources().getDrawable(R.drawable.silent_on);
    } else {
        silentImage = getResources().getDrawable(R.drawable.silent_off);
    }
    imageView.setImageDrawable(silentImage);
}

如果有人能帮忙,我会很高兴,谢谢。

最佳答案

自 API 22 起,您应该调用 ContextCompat.getDrawable(context, R.drawable.***) 而不是 getResources().getDrawable(R.drawable.***).

可以查看ContextCompat的文档从支持库获取更多详细信息。

关于Android getDrawable() 已弃用/如何使用 Android getDrawable(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32765906/

相关文章:

Android - 在 RecyclerView 中重新创建自定义动态生成的复合 View ,导致性能不佳

android - 未找到可绘制/ic_launcher_foreground。如何找到那个xml文件

android - 如何从 ListView 中获取一个参数

Android 在下一个选项卡中显示和保存数据

Android - 动画 Intent 但忽略 View

android - 如何以编程方式设置 View 的最小高度?

android - HCE 是否支持 ISO/IEC 14443-3 B 类?

android - 应用未显示在Google Play应用中,但通过网络浏览器显示

android - 以编程方式显示 NavigationView MenuItems 全息指示器

android - 如何修复:android.app.RemoteServiceException:从程序包中发布的错误通知*:无法创建图标:StatusBarIcon