2012-09-11 8 views
26

मैं कुछ इस तरह की कोशिश की, लेकिन मैं अटक:कैसे वर्तमान थीम से पृष्ठभूमि रंग प्राप्त करने के लिए प्रोग्राम के रूप में

TypedValue typedValue = new TypedValue(); 
if (this.parentActivity.getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true)) 
{ 
    // how to get color? 
} 
+9

: पृष्ठभूमि = "एंड्रॉयड: attr/colorBackground" – ademar111190

+2

मैं परीक्षण किया है और निर्धारित किया है कि 'एंड्रॉयड: attr/colorBackground'' styles.xml' के आइटम से संबंधित '<आइटम नाम =" एंड्रॉयड : रंगबैकग्राउंड "> @ रंग/आपका रंग यहाँ'। –

+0

@ ademar111190 यह जवाब होना चाहिए !!!! – Alexandr

उत्तर

52

आप प्राप्त कर सकते हैं वर्तमान थीम से पृष्ठभूमि रंग (या Drawable) द्वारा:

TypedValue a = new TypedValue(); 
getTheme().resolveAttribute(android.R.attr.windowBackground, a, true); 
if (a.type >= TypedValue.TYPE_FIRST_COLOR_INT && a.type <= TypedValue.TYPE_LAST_COLOR_INT) { 
    // windowBackground is a color 
    int color = a.data; 
} else { 
    // windowBackground is not a color, probably a drawable 
    Drawable d = activity.getResources().getDrawable(a.resourceId); 
} 
+2

ग्रेट उत्तर। यदि किसी और के द्वारा इस पर ट्राइप किया गया है, तो ज़ैमरिन पर आपको संसाधन के लिए संसाधन का उपयोग करना होगा। उदाहरण के लिए, संसाधन। सटेबल। MyTheme_primaryAccentColor। यह शायद एंड्रॉइड देशी विकास पर भी लागू होता है। यही है, सीधे थीम के बजाय attr फ़ाइल/ऑब्जेक्ट का संदर्भ लें। मुझे अंतर समझ में नहीं आता है, लेकिन बाद वाला ऐसा लगता है कि यह सही विकल्प होगा लेकिन नहीं था। – pbarranis

4

आप का उपयोग करके अपने विषय के संसाधनों प्राप्त कर सकते हैं:

TypedArray a = getTheme().obtainStyledAttributes(R.style.ThemeName, new int[] {R.attr.attribute_name});  
int attributeResourceId = a.getResourceId(0, 0); 
+0

मैंने यह कोशिश की: TypedArray a = this.parentActivity.getTheme()। GetStyledAttributes (android.R.attr.windowBackground, नया int [] {android.R.attr.windowBackground}); \t \t int विशेषताResourceId = a.getResourceId (0, 0); । \t \t \t \t पूर्णांक aaa = this.parentActivity.getResources() getColor (attributeResourceId); लेकिन यह काम नहीं कर रहा है। मुझे अपवाद मिलता है। –

+0

ओह! आपको क्या अपवाद मिलता है? – Swayam

+0

09-12 12: 05: 34.864: ई/एंड्रॉइड रनटाइम (32137): अंतिम अपवाद: मुख्य 09-12 12: 05: 34.864: ई/एंड्रॉइड रनटाइम (32137): android.content.res.Resources $ NotFoundException: फ़ाइल res /drawable/screen_background_selector_light.xml रंग राज्य सूची संसाधन आईडी # 0x10804a8 09-12 12: 05: 34.864: ई/एंड्रॉइड रनटाइम (32137): \t android.content.res.Resources.loadColorStateList (Resources.java) 09- 12 12: 05: 34.864: ई/एंड्रॉइड रनटाइम (32137): \t android.content.res.Resources.getColor (Resources.java) ... –

1

अपने qoustion के लिए सबसे आसान तरीका है:

TypedValue typedValue = new TypedValue(); 
if (this.parentActivity.getTheme().resolveAttribute(android.R.attr.windowBackground, typedValue, true)) 
{ 
    // how to get color? 
    int colorWindowBackground = typedValue.data;// **just add this line to your code!!** 
} 
एक्सएमएल एंड्रॉयड पर
संबंधित मुद्दे

 संबंधित मुद्दे