2010-01-22 14 views
39

के साथ प्रयुक्त inflater का उपयोग किया गया है मेरे पास टेक्स्टव्यू के लिए पाठ रंग को लाल रंग के रूप में निर्दिष्ट किया गया है।थीम/स्टाइल लागू नहीं किया गया है जब अनुप्रयोगकॉन्टेक्स्ट

मैं TextView को तुरंत चालू करने के लिए लेआउटइनफ्लेटर का उपयोग कर रहा हूं। समस्या यह है कि अनुप्रयोग दृश्यों का उपयोग करके बनाए गए inflater जब शैलियों को TextView पर लागू नहीं किया जाता है - रंग लाल नहीं होता है। जब सभी लेआउट इन्फ्लेटर गतिविधि का उपयोग करके बनाया गया तो ठीक काम करता है।

ऐसा क्यों होता है, और कैसे तय किया जा सकता है?

/res/values/styles.xml:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="MyTheme"> 
     <item name="android:textViewStyle">@style/MyTextView</item> 
    </style> 

    <style name="MyTextView" parent="@android:style/Widget.TextView"> 
     <item name="android:textColor">#f00</item> 
    </style> 
</resources> 

AndroidManifest.xml:

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:theme="@style/MyTheme" 
    > 

कोड:

public class A extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.test_a); 

     final LayoutInflater goodInflater = getInflater((Activity)this); 
     final LayoutInflater badInflater = getInflater(getApplicationContext()); 
     final LinearLayout container = (LinearLayout)findViewById(R.id.container); 

     findViewById(R.id.add_with_appContext).setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       add(container, badInflater); // Creates gray TextView 
      }    
     }); 

     findViewById(R.id.add_with_activity).setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       add(container, goodInflater); // Creates red TextView 
      }    
     }); 
    } 

    private LayoutInflater getInflater(Context context) { 
     return (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    private void add(LinearLayout container, LayoutInflater inflater) { 
     inflater.inflate(R.layout.my_template, container, true); 
    } 
} 

/res/layout/test_a.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <Button 
     android:text="Add with AppContext" 
     android:id="@+id/add_with_appContext" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

    <Button 
     android:text="Add with Activity" 
     android:id="@+id/add_with_activity" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

    <LinearLayout 
     android:id="@+id/container" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     /> 

</LinearLayout> 

/res/layout/my_template.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    > 

    <TextView 
     android:id="@+id/text" 
     android:text="Some text..." 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
    /> 

</LinearLayout> 

उत्तर

45

समाधान # 1

बढ़ विधि वैकल्पिक 'ViewGroup जड़' तर्क स्वीकार करता है:

public View inflate (int resource, ViewGroup root, boolean attachToRoot) 

हम करने के लिए मूल्य है, तो 'रूट' पैरामीटर के रूप में पास करें, इसलिए हम इसका उपयोग 'गतिविधि संदर्भ' प्राप्त करने के लिए कर सकते हैं, जहां से हम सही लेआउट इन्फ्लेटर प्राप्त कर सकते हैं:

ViewGroup root > activity context > LayoutInflater 

तो मेरी कोड हो सकता है:

private void add(LinearLayout container) { 
    LayoutInflater inflater = getInflater(container.getContext()); 
    inflater.inflate(R.layout.my_template, container, true); 
} 

समाधान # 2

बस प्रोग्राम के रूप में आवेदन संदर्भ विषय स्थापित करने के लिए करने की कोशिश की, और यह काम करता है:

getApplicationContext().setTheme(R.style.MyTheme); 

मुझे लगता है कि यह मार्कअप की अपेक्षा करने के लिए तार्किक था:

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:theme="@style/MyTheme" 
    > 

इसे स्वचालित रूप से सेट करने के लिए, लेकिन ऐसा नहीं है।

+0

सर में आपने गतिविधि का एक ही विषय रखने के लिए व्यक्तिगत रूप से किया है। सर आपने अपना दिन बचाया। धन्यवाद –

+0

यह इतना गलत है। ऐसा एक कारण है जिस तरह से आप सोचते हैं कि यह काम करता है: यह उस तरह से काम नहीं करना चाहिए। टैग पर थीम विशेषता केवल डिफ़ॉल्ट है जो सभी गतिविधियों पर लागू होती है। उदाहरण के लिए, कई अन्य चीजें वास्तव में ऐप संदर्भ के साथ काम नहीं करती हैं, उदाहरण के लिए, आप संसाधनों को रिसाव करेंगे जो कॉन्फ़िगरेशन परिवर्तनों के जवाब में बदलते हैं, आदि – dcow

+0

समाधान # 2 एक आकर्षण की तरह काम करता है। मेरा आवेदन अब खूबसूरत लग रहा है। धन्यवाद! – Mauker

31

विचारों को बढ़ाने के लिए कभी भी एप्लिकेशन संदर्भ का उपयोग न करें, क्योंकि स्टाइल इस संदर्भ के साथ काम नहीं करता है। विचारों के साथ खेलते समय हमेशा गतिविधि के संदर्भ का उपयोग करें। एकमात्र अपवाद तब होता है जब आपको किसी सेवा से रिमोट व्यू बनाने की आवश्यकता होती है।

विभिन्न प्रकार के संदर्भों और उनकी क्षमताओं के बारे में अधिक जानकारी in this excellent article मिल सकती है।

+2

यह वास्तव में सच नहीं है, दृश्य बनाने के लिए एप्लिकेशन संदर्भ का उपयोग करके स्मृति रिसाव से बचने के लिए भी सिफारिश की जाती है जिसके साथ मैं अभी व्यवहार कर रहा हूं। – David

+2

इस पर निर्भर करता है कि आपके विचारों का उपयोग कैसे किया जाता है। यदि आपके विचार किसी गतिविधि का हिस्सा नहीं हैं तो आपको पाठ्यक्रम के संदर्भ के संदर्भ का उपयोग नहीं करना चाहिए। – BladeCoder

+0

महोदय, आपने मेरा दिन बचाया। धन्यवाद –

0

मैं आम तौर पर कस्टम दृश्य को बढ़ाने पर इस मुद्दे पर आ जाता हूं।कस्टमव्यू

public class CustomView extends ViewGroup{ 

public CustomView (Context context) { 
    super(context); 
    init(context); 
} 

public CustomView (Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(context); 
} 

public CustomView (Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    init(context); 
} 

@TargetApi(21) 
public CustomView (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
    super(context, attrs, defStyleAttr, defStyleRes); 
    init(context); 
} 

private void init(Context context) { 
    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = mInflater.inflate(R.layout.review_list_item, this, true); 
    //rest of view initialization  
} 
} 
संबंधित मुद्दे