Java Android:getDrawable和其他没有版本检查的资源?
我必须在我的代码中经常进行这种检查,我想知道是否有一种干净的方法可以获取资源,而不必编写if语句来检查版本Java Android:getDrawable和其他没有版本检查的资源?,java,android,Java,Android,我必须在我的代码中经常进行这种检查,我想知道是否有一种干净的方法可以获取资源,而不必编写if语句来检查版本 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { imageView.setImageDrawable(getDrawable(R.drawable.ic_circled_v)); } else { imageView.setImageDrawable(getResources().getDra
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));
}
使用注释、@requireApi或@targetApi这是获得可绘制资源并保持向后兼容性的方法 这是您希望在活动中使用的代码:
imageView.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.icon_heart_fill_red));
其中,图标\u heart\u fill\u red是您的绘图设备的名称。这里很好地解释了各种compat选项。