2012-01-06 15 views
5

मैं अपने आवेदन में थीम स्विचिंग को लागू करने के लिए कस्टम विशेषताओं का उपयोग कर रहा हूं। मैं निम्न विशेषता को परिभाषित किया है: इस प्रकार परिभाषित किया गया हैसेट टेक्स्ट एडाप्टर कोड संदर्भ संदर्भ कस्टमर

<style name="NI_AppTheme.Dark"> 
    <item name="TextAppearance_Footer">@style/Footer</item> 
</style> 

@style/Footer:

<style name="Footer" parent="@android:style/TextAppearance.Large"> 
    <item name="android:textColor">#00FF00</item> // Green 
</style> 

अब अगर मैं करने की कोशिश

<resources> 
    <attr name="TextAppearance_Footer" format="reference"></attr> 
</resources> 

मैं दो विषयों जो इस विशेषता को अलग ढंग से परिभाषित किया है इस शैली को TextView पर सेट करें:

textView.setTextAppearance(this, R.attr.TextAppearance_Footer); 

यह काम नहीं करता है (यानी। टेक्स्ट को ग्रीन में सेट नहीं करता है)। हालांकि, अगर मैं xml के माध्यम से टेक्स्ट उपस्थिति निर्दिष्ट करता हूं:

android:textAppearance="?TextAppearance_Footer" 

यह ठीक काम करता है। मैं क्या लापता हो सकता है? मुझे विशेषताओं को सेट करने की आवश्यकता है क्योंकि मैं गतिशील रूप से विषयों के बीच स्विच करने में सक्षम होना चाहता हूं।

अतिरिक्त जानकारी:

अगर मैं का उपयोग करें:

textView.setTextAppearance(this, R.style.NI_AppTheme.Dark); 

यह सब सही काम करने के लिए लगता है।

संपादित करें: परीक्षण कार्य समाधान (धन्यवाद @nininho):

Resources.Theme theme = getTheme(); 
TypedValue styleID = new TypedValue(); 
if (theme.resolveAttribute(R.attr.Channel_Title_Style, styleID, true)) { 
    channelTitle.setTextAppearance(this, styleID.data); 
} 

उत्तर

11

का प्रयोग क्यों नहीं:

textView.setTextAppearance(this, R.style.Footer); 

मुझे लगता है कि textAppearance एक शैली होना चाहिए।

संपादित करें:

हो सकता है कि आप इस प्रयास करना चाहिए:

TypedArray a = context.obtainStyledAttributes(attrs, 
new int[] { R.attr.TextAppearance_Footer }); 

int id = a.getResourceId(R.attr.TextAppearance_Footer, defValue); 
textView.setTextAppearance(this, id); 

संपादित करें: सही परीक्षण कोड:

Resources.Theme theme = getTheme(); 
TypedValue styleID = new TypedValue(); 
if (theme.resolveAttribute(R.attr.Channel_Title_Style, styleID, true)) { 
    channelTitle.setTextAppearance(this, styleID.data); 
} 
+0

क्योंकि शायद वह थीम के माध्यम से इसे चुनना चाहता है, और सीधे R.style.Footer का उपयोग नहीं करना चाहता। – aromero

+0

तो उसे विषय का उपयोग करना चाहिए, लेकिन सवाल से वह इसका उपयोग नहीं करना चाहता (पता नहीं क्यों) –

+0

यह सुनिश्चित नहीं है कि यह मामला है। वह क्या करने की कोशिश कर रहा है वह टेक्स्ट एपपेरेंस शैली को विषय पर प्रस्तुत करता है। TextAppearance_Footer वह विशेषता है जो लिंकिंग करता है, उदाहरण के लिए, मान लीजिए कि आपके पास थीम 1 और थीम 2 है। थीम 1 टेक्स्टएपियरेंस_फूटर => टेक्स्टएपियरेंस 1 और थीम 2 कहेंगे टेक्स्टएपियरेंस_फूटर => टेक्स्टएपियरेंस 2। अपने विगेट्स में आप केवल टेक्स्ट एपियरेंस => TextAppearance_Footer कहते हैं।आपको जो मिलता है वह वर्तमान थीम – aromero

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