gpt4 book ai didi

java - 安卓 : getDrawable and other resources without a version check?

转载 作者:太空宇宙 更新时间:2023-11-03 12:26:45 24 4
gpt4 key购买 nike

我必须在我的代码中经常进行此类检查,我想知道是否有一种干净的方法来获取资源而无需编写 if 语句来检查版本。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
{
imageView.setImageDrawable(getDrawable(R.drawable.ic_circled_v));
}
else
{
imageView.setImageDrawable(getResources().getDrawable(R.drawable.ic_circled_v));
}

最佳答案

这是您获取可绘制资源并保持向后兼容性的方式。

这是您要在 Activity 中使用的代码:

imageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.icon_heart_fill_red));

其中 icon_heart_fill_red 是您的可绘制对象的名称。

关于java - 安卓 : getDrawable and other resources without a version check?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50540124/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com