With new android API 22 getResources().getDrawable() is now deprecated. Now the best approach is to use only getDrawable().
使用新的android API 22 getResources(). getdrawable()現在已被棄用。現在最好的方法是只使用getDrawable()。
What change?
改變什么?
9 个解决方案
#1
747
You have some options to handle this deprecation the right (and future proof) way, depending on which kind of drawable you are loading:
您可以使用一些選項來處理這個問題(以及未來的證明),這取決於您所加載的是哪一種類型:
A) drawables with theme attributes
帶有主題屬性的drawables。
ContextCompat.getDrawable(getActivity(), R.drawable.name);
You'll obtain a styled Drawable as your Activity theme instructs. This is probably what you need.
您將獲得一個風格的Drawable作為您的活動主題指示。這可能就是你所需要的。
B) drawables without theme attributes
B)沒有主題屬性的drawables。
ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
You'll get your unstyled drawable the old way. Please note: ResourcesCompat.getDrawable() is not deprecated!
你會用舊的方式來畫你的風格。請注意:ResourcesCompat.getDrawable()並沒有被棄用!
EXTRA) drawables with theme attributes from another theme
額外的)從另一個主題的主題屬性的drawables。
ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
#2
706
Edit: see my blog post on the subject for a more complete explanation
You should use the following code from the support library instead:
您應該從支持庫中使用以下代碼:
ContextCompat.getDrawable(context, R.drawable.***)
Using this method is equivalent to calling:
使用該方法等效於調用:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return resources.getDrawable(id, context.getTheme());
} else {
return resources.getDrawable(id);
}
As of API 21, you should use the getDrawable(int, Theme) method instead of getDrawable(int), as it allows you to fetch a drawable object associated with a particular resource ID for the given screen density/theme. Calling the deprecated getDrawable(int) method is equivalent to calling getDrawable(int, null).
在API 21中,您應該使用getDrawable(int, Theme)方法而不是getDrawable(int),因為它允許您獲取與給定屏幕密度/主題的特定資源ID相關聯的drawable對象。調用已棄用的getDrawable(int)方法等價於調用getDrawable(int, null)。
#3
135
Replace this line : getResources().getDrawable(R.drawable.your_drawable)
替換這一行:getResources().getDrawable(R.drawable.your_drawable)
with ResourcesCompat.getDrawable(getResources(), R.drawable.your_drawable, null)
R.drawable ResourcesCompat.getDrawable(getresource()。your_drawable零)
EDIT
編輯
ResourcesCompat is also deprecated now. But you can use this:
現在,資源compat也被棄用。但是你可以用這個:
ContextCompat.getDrawable(this, R.drawable.your_drawable) (Here this is the context)
ContextCompat。getDrawable(這個,R.drawable.your_drawable)(這里是上下文)
for more details follow this link: ContextCompat
更多細節請點擊這個鏈接:ContextCompat。
#4
24
getResources().getDrawable() was deprecated in API level 22. Now we must add the theme:
getDrawable(int id、資源。主題主題)(添加於API第21級)
This is an example:
這是一個例子:
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage, getApplicationContext().getTheme()));
This is an example how to validate for later versions:
這是一個如何驗證后續版本的示例:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //>= API 21
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage, getApplicationContext().getTheme()));
} else {
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage));
}
#5
2
You can use
您可以使用
ContextCompat.getDrawable(getApplicationContext(),R.drawable.example);
that's work for me
這對我的工作
#6
1
Just an example of how I fixed the problem in an array to load a listView, hope it helps.
這只是一個例子,說明我如何在數組中解決這個問題來加載listView,希望它能有所幫助。
mItems = new ArrayList();
// Resources resources = getResources();
// mItems.add(new ListViewItem(resources.getDrawable(R.drawable.az_lgo), getString(R.string.st_az), getString(R.string.all_nums)));
// mItems.add(new ListViewItem(resources.getDrawable(R.drawable.ca_lgo), getString(R.string.st_ca), getString(R.string.all_nums)));
// mItems.add(new ListViewItem(resources.getDrawable(R.drawable.co_lgo), getString(R.string.st_co), getString(R.string.all_nums)));
mItems.add(new ListViewItem(ResourcesCompat.getDrawable(getResources(), R.drawable.az_lgo, null), getString(R.string.st_az), getString(R.string.all_nums)));
mItems.add(new ListViewItem(ResourcesCompat.getDrawable(getResources(), R.drawable.ca_lgo, null), getString(R.string.st_ca), getString(R.string.all_nums)));
mItems.add(new ListViewItem(ResourcesCompat.getDrawable(getResources(), R.drawable.co_lgo, null), getString(R.string.st_co), getString(R.string.all_nums)));
#7
1
Try this:
試試這個:
public static List getCatalog(Resources res){
if(catalog == null) {
catalog.add(new Product("Dead or Alive", res
.getDrawable(R.drawable.product_salmon),
"Dead or Alive by Tom Clancy with Grant Blackwood", 29.99));
catalog.add(new Product("Switch", res
.getDrawable(R.drawable.switchbook),
"Switch by Chip Heath and Dan Heath", 24.99));
catalog.add(new Product("Watchmen", res
.getDrawable(R.drawable.watchmen),
"Watchmen by Alan Moore and Dave Gibbons", 14.99));
}
}
#8
0
Build.VERSION_CODES.LOLLIPOP should now be changed to BuildVersionCodes.Lollipop i.e:
Build.VERSION_CODES。現在,LOLLIPOP應該更改為buildversioncode。棒棒糖即:
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);
}
#9
0
en api level 14 marker.setIcon(ResourcesCompat.getDrawable(getResources(),R.drawable.miubicacion, null));
api級別14 marker.setIcon(ResourcesCompat.getDrawable(getResources(),R.drawable)。miubicacion,null));