Android getResources ().getdrawable () deprecated API 22

Z nowym android API 22 getResources().getDrawable() jest teraz przestarzały. Teraz najlepszym rozwiązaniem jest użycie tylko getDrawable().

Jaka zmiana?
Author: MiguelHincapieC, 2015-03-13

10 answers

Masz kilka opcji, aby poradzić sobie z tą deprecjacją w odpowiedni (i future proof) sposób, w zależności od tego, jakiego rodzaju drawable ładujesz:


A) drawables with Theme attributes

ContextCompat.getDrawable(getActivity(), R.drawable.name);

Otrzymasz stylizowany rysunek zgodnie z instrukcjami dotyczącymi motywu aktywności. Pewnie tego potrzebujesz.


B) drawables bez atrybutów motywu

ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
/ Align = "left" / Uwaga: ResourcesCompat.getDrawable() is not deprecated!

EXTRA) drawables with Theme attributes from another theme

ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
 819
Author: araks,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-11 17:38:52

Edit: zobacz mój wpis na blogu na ten temat, aby uzyskać pełniejsze Wyjaśnienie


Powinieneś użyć następującego kodu z biblioteki wsparcia:

ContextCompat.getDrawable(context, R.drawable.***)

Użycie tej metody jest równoważne wywołaniu:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    return resources.getDrawable(id, context.getTheme());
} else {
    return resources.getDrawable(id);
}

Począwszy od API 21, powinieneś używać metody getDrawable(int, Theme) zamiast getDrawable(int), ponieważ pozwala ona na pobranie rysowalnego obiektu powiązanego z określonym identyfikatorem zasobu dla danej gęstości ekranu/motywu. Wywołanie przestarzałej metody getDrawable(int) jest równoważne wywołanie getDrawable(int, null).

 718
Author: Alex Lockwood,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-06 02:50:30

Zastąp tę linię : getResources().getDrawable(R.drawable.your_drawable)

Z ResourcesCompat.getDrawable(getResources(), R.drawable.your_drawable, null)

Edytuj

ResourcesCompat jest również obecnie przestarzały. Ale możesz użyć tego:

ContextCompat.getDrawable(this, R.drawable.your_drawable) (Tutaj this jest kontekst)

Więcej szczegółów pod tym linkiem: ContextCompat

 138
Author: vincent091,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-08-29 20:00:40

getResources().getDrawable() został wycofany z API na poziomie 22. Teraz musimy dodać temat:

GetDrawable (int id, Resources.Theme theme) (dodany w API poziom 21)

Oto przykład:

myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage, getApplicationContext().getTheme()));

To jest przykład jak sprawdzić poprawność dla późniejszych wersji:

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));
}
 25
Author: Jorgesys,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-17 18:32:57

Możesz użyć

ContextCompat.getDrawable(getApplicationContext(),R.drawable.example);

That ' s work for me

 2
Author: Dasser Basyouni,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-12-26 04:36:32

Tylko przykład jak naprawiłem problem w tablicy, aby załadować listView, mam nadzieję, że to pomoże.

 mItems = new ArrayList<ListViewItem>();
//    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)));
 1
Author: Jay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-12 17:40:50

Spróbuj tego:

public static List<ProductActivity> 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));
    }
}
 1
Author: syakirin mohd nor,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-09 15:06:23

EN API poziom 14

marker.setIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.miubicacion, null));
 0
Author: Jaime López Romero,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-03 13:42:06

Zbuduj.VERSION_CODES.LOLLIPOP powinien teraz zostać zmieniony na BuildVersionCodes.Lizak i. e:

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);
}
 0
Author: Ryan Herman,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-03 13:42:48

Jeśli kierujesz SDK > 21 (lollipop lub 5.0) użyj

context.getDrawable(R.drawable.your_drawable_name)

Zobacz dokumenty

 0
Author: Adeel Ahmad,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-24 20:21:03