नए Android एपीआई 22 getResources().getDrawable()
के साथ अब पदावनत हो गया है। अब सबसे अच्छा तरीका केवल उपयोग करना है getDrawable()
।
किया बदल गया?
Resources#getDrawable(int)
और दोनों Resources#getColor(int)
को हटा दिया गया।
नए Android एपीआई 22 getResources().getDrawable()
के साथ अब पदावनत हो गया है। अब सबसे अच्छा तरीका केवल उपयोग करना है getDrawable()
।
किया बदल गया?
Resources#getDrawable(int)
और दोनों Resources#getColor(int)
को हटा दिया गया।
जवाबों:
आपके पास इस विकल्प को सही (और भविष्य के सबूत ) तरीके से संभालने के लिए कुछ विकल्प हैं , जो इस बात पर निर्भर करता है कि आप किस प्रकार का ड्रॉबल लोड कर रहे हैं:
ए) drawables साथ विषय गुण
ContextCompat.getDrawable(getActivity(), R.drawable.name);
जैसा कि आपकी गतिविधि थीम निर्देश देती है, आप एक स्टाइल ड्राएबल प्राप्त करेंगे। शायद यही आपकी जरूरत है।
बी) drawables बिना विषय गुण
ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);
आप अपने पुराने रास्ते को अस्थिर कर पाएंगे। कृपया ध्यान दें: ResourcesCompat.getDrawable()
है नहीं पदावनत!
अतिरिक्त) एक अन्य विषय से विषय विशेषताओं के साथ चित्र बनाता है
ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);
आपको इसके बजाय समर्थन लाइब्रेरी से निम्न कोड का उपयोग करना चाहिए:
ContextCompat.getDrawable(context, R.drawable.***)
इस पद्धति का उपयोग करना कॉल करने के बराबर है:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return resources.getDrawable(id, context.getTheme());
} else {
return resources.getDrawable(id);
}
एपीआई 21 के रूप में, आपको getDrawable(int, Theme)
इसके बजाय विधि का उपयोग करना चाहिए getDrawable(int)
, क्योंकि यह आपको दिए गए स्क्रीन घनत्व / थीम के लिए एक विशेष संसाधन आईडी से जुड़ी एक आकर्षित करने योग्य वस्तु लाने की अनुमति देता है। पदावनत getDrawable(int)
विधि को कॉल करना कॉल करने के बराबर है getDrawable(int, null)
।
getDrawable (int id)
पद्धति को भी संदर्भित करता है Context
। यह वैसा ही है getResources().getDrawable(id, getTheme());
और नए API का भी उपयोग करता है।
getDrawable(int, Resources.Theme)
।
इस पंक्ति को बदलें:
getResources().getDrawable(R.drawable.your_drawable)
साथ में ResourcesCompat.getDrawable(getResources(), R.drawable.your_drawable, null)
संपादित करें
ResourcesCompat
अब भी वंचित है। लेकिन आप इसका उपयोग कर सकते हैं:
ContextCompat.getDrawable(this, R.drawable.your_drawable)
(यहाँ this
संदर्भ है)
अधिक जानकारी के लिए इस लिंक का अनुसरण करें: ContextCompat
getResources().getDrawable()
एपीआई स्तर 22 में पदावनत किया गया था। अब हमें विषय जोड़ना चाहिए:getDrawable (int id, Resources.Theme विषय) (एपीआई स्तर 21 में जोड़ा गया)
यह एक उदाहरण है:
myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage, getApplicationContext().getTheme()));
यह एक उदाहरण है कि बाद के संस्करणों के लिए कैसे मान्य किया जाए:
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));
}
Build.VERSION_CODES.LOLLIPOP is API 21
, तो यह if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1)
या नहीं होना चाहिए if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP)
? कोई बात नहीं। नीचे से "विधि को एपीआई 21 में जोड़ा गया था, लेकिन एपीआई 22 तक इसे अपदस्थ नहीं किया गया था :)"
आप उपयोग कर सकते हैं
ContextCompat.getDrawable(getApplicationContext(),R.drawable.example);
मेरे लिए वह काम है
किसी सूचीदृश्य को लोड करने के लिए मैंने सरणी में समस्या को कैसे ठीक किया, इसका एक उदाहरण है, आशा है कि यह मदद करता है।
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)));
इसे इस्तेमाल करे:
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));
}
}
यदि आप SDK> 21 (लॉलीपॉप या 5.0) उपयोग को लक्षित कर रहे हैं
context.getDrawable(R.drawable.your_drawable_name)
getDrawable (int drawable) API स्तर 22 में पदावनत किया गया है। संदर्भ के लिए इस लिंक को देखें ।
अब इस समस्या को हल करने के लिए हमें एक नए कंस्ट्रक्टर को आईडी के साथ पास करना होगा जैसे: -
getDrawable(int id, Resources.Theme theme)
समाधान के लिए इस तरह करें: -
जावा में: -
ContextCompat.getDrawable(getActivity(), R.drawable.name);
या
imgProfile.setImageDrawable(getResources().getDrawable(R.drawable.img_prof, getApplicationContext().getTheme()));
कोटलिन में: -
rel_week.background=ContextCompat.getDrawable(this.requireContext(), R.color.colorWhite)
या
rel_day.background=resources.getDrawable(R.drawable.ic_home, context?.theme)
आशा है कि यह आपकी मदद करेगा। धन्यवाद
hi 14 का स्तर
marker.setIcon(ResourcesCompat.getDrawable(getResources(), R.drawable.miubicacion, null));
अब आपको इस तरह से लागू करने की आवश्यकता है
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //>= API 21
//
} else {
//
}
कोड की एकल पंक्ति के बाद पर्याप्त है, सब कुछ ContextCompat.getDrawable द्वारा ध्यान रखेगा
ContextCompat.getDrawable(this, R.drawable.your_drawable_file)
कुछ लोगों के लिए जो इस धागे के सुझाव को लागू करने के बाद भी हल करने के लिए इसे प्राप्त करते हैं (मैं इस तरह एक हुआ करता था) इस लाइन को अपने आवेदन वर्ग, ऑनक्रिएट () विधि में जोड़ें
AppCompatDelegate.setCompatVectorFromResourcesEnabled(true)
जैसा कि यहां और यहां बताया गया है कि कभी-कभी संसाधनों से वैक्टर तक पहुंचने की आवश्यकता होती है, खासकर जब आप मेनू आइटम, आदि के साथ काम कर रहे होते हैं
कोटलिन में आप एक्सटेंशन का उपयोग कर सकते हैं
fun Context.getMyDrawable(id : Int) : Drawable?{
return ContextCompat.getDrawable(this, id)
}
फिर उपयोग करें
context.getMyDrawable(R.drawable.my_icon)
Build.VERSION_CODES.LOLLIPOP को अब BuildVersionCodes.ollipop पर बदल दिया जाना चाहिए:
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);
}
BuildVersionCodes
Xamarin के लिए एक वर्ग विशिष्ट नहीं है?
getDrawable (int id)
वर्ग की विधिResources
को पदावनत किया जाता है। अब आपकोgetDrawable (int id, Resources.Theme theme)
नए थीम पैरामीटर के साथ विधि का उपयोग करना चाहिए ।