对于新的android API 22,getResources().getDrawable()现在已被弃用。现在最好的方法是只使用getDrawable()。
什么改变了?
对于新的android API 22,getResources().getDrawable()现在已被弃用。现在最好的方法是只使用getDrawable()。
什么改变了?
当前回答
en-api级别14
marker.setIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.miubicacion, null));
其他回答
Build.VERSION_CODES.LOLLIOP现在应更改为BuildVersionCodes.LOLLIPOP即:
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop) {
this.Control.Background = this.Resources.GetDrawable(Resource.Drawable.AddBorder, Context.Theme);
} else {
this.Control.Background = this.Resources.GetDrawable(Resource.Drawable.AddBorder);
}
getDrawable(int drawable)在API级别22中被弃用。有关参考,请参阅此链接。
现在,为了解决这个问题,我们必须通过一个新的构造器以及id,如:-
getDrawable(int id, Resources.Theme theme)
对于解决方案,请这样做:-
在Java中:-
ContextCompat.getDrawable(getActivity(), R.drawable.name);
or
imgProfile.setImageDrawable(getResources().getDrawable(R.drawable.img_prof, getApplicationContext().getTheme()));
在Kotlin:-
rel_week.background=ContextCompat.getDrawable(this.requireContext(), R.color.colorWhite)
or
rel_day.background=resources.getDrawable(R.drawable.ic_home, context?.theme)
希望这对你有帮助。谢谢。
您可以使用
ContextCompat.getDrawable(getApplicationContext(),R.drawable.example);
这是我的工作
试试这个
ContextCompat.getDrawable(getActivity(), R.drawable.drawable_resource_name);
现在您需要这样实现
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //>= API 21
//
} else {
//
}
只需执行一行代码就足够了,ContextCompat.getDrawable将处理所有问题
ContextCompat.getDrawable(this, R.drawable.your_drawable_file)