2013-02-12 19 views
8

के आधार पर परिवर्तन बदलता है मैं अपने पूरे आवेदन में अपने Views के लिए अजीब स्वरूपण शैली असंगतताओं का कारण ढूंढने की कोशिश कर रहा हूं और मुझे लगता है कि मैंने इसे इस उदाहरण के साथ संकुचित कर दिया है।एंड्रॉइड होलो लाइट स्टाइल बदलते संदर्भ

मैं Context निर्माण में आपूर्ति की बदलती ठीक उसी प्रक्रिया और केवल के साथ विभिन्न Views रों के दो बराबर लेआउट की स्थापना की। पहले सेट में, प्रत्येक View, Activity.getApplicationContext() के माध्यम से आवेदन के संदर्भ के साथ बनाया है, जबकि दूसरे सेट में View रों this के माध्यम से गतिविधि के संदर्भ तंग आ चुके हैं है।

परिणाम एकदम अलग है:

enter image description here

क्यों आवेदन संदर्भ का उपयोग करने पर कोई सुझाव कचरा का कारण बनता है (और असंगत - रंग सफेद के साथ-साथ ग्रे कर रहे हैं) स्क्रीनशॉट में देखा स्वरूपण?

अभ्यास कोड:

import android.os.Bundle; 
import android.app.Activity; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.Spinner; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // TextViews 
     TextView tv1 = new TextView(getApplicationContext()); 
     tv1.setText("With Application context"); 
     TextView tv2 = new TextView(this); 
     tv2.setText("With Activity context");  

     // Spinners 
     Spinner sp1 = new Spinner(getApplicationContext()); 
     sp1.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, new String[] {"App context 1", "App context 2", "App context 3"})); 
     Spinner sp2 = new Spinner(this); 
     sp2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, new String[] {"Act context 1", "Act context 2", "Act context 3"})); 

     // Edittexts 
     EditText et1 = new EditText(getApplicationContext()); 
     et1.setText("Application Context"); 
     EditText et2 = new EditText(this); 
     et2.setText("Activity Context");   

     // Buttons 
     Button b1 = new Button(getApplicationContext()); 
     b1.setText("Application Context"); 
     Button b2 = new Button(this); 
     b2.setText("Activity Context");  

     // Layout structure 
     LinearLayout ll = new LinearLayout(this); 
     ll.setOrientation(LinearLayout.VERTICAL); 

     ll.addView(tv1); 
     ll.addView(sp1); 
     ll.addView(et1); 
     ll.addView(b1); 
     ll.addView(tv2); 
     ll.addView(sp2); 
     ll.addView(et2); 
     ll.addView(b2); 

     setContentView(ll); 
    } 

} 

मैनिफ़ेस्ट:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.test.test" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="11" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@android:style/Theme.Holo.Light" > 
     <activity 
      android:name="com.test.test.MainActivity" 
      android:label="@string/app_name" 
      android:theme="@android:style/Theme.Holo.Light" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 
+0

आप लेआउट के साथ 'applicationContext' का उपयोग कर की कोशिश n क्या साथ ही? 'लीनियर लयआउट ll = new LinearLayout (getAplicationContext());'। मेरा मानना ​​है कि आप समझते हैं कि 'getAplicationContext()' का उपयोग करना गलत है, लेकिन इस अजीब स्वरूपण असंगतता के पीछे कारण सीखना दिलचस्प है। – iTech

+0

आपको लेआउट/दृश्य दृश्यों को बढ़ाने के लिए एप्लिकेशन (या आधार) संदर्भ का उपयोग नहीं करना चाहिए, गतिविधि संदर्भ इस मामले में उपयोग करने वाला एकमात्र सही है। –

उत्तर

12

एंड्रॉयड स्रोतों में getApplicationContext() रिटर्न आवेदन वस्तु जो ContextWrapper फैली हुई है, इसलिए getApplicationContext() का उपयोग कर आप ContextWrapper उपवर्ग से गुजर रहे हैं, लेकिन जब आप this पास करें, आपपास कर रहे हैंऑब्जेक्ट जो ContextThemeWrapper बढ़ाता है ताकि आप ContextThemeWrapper सबक्लास पास कर रहे हों। अब ContextWrapper और ContextThemeWrapper के बीच अंतर यह है कि ContextWrappersimply delegates all of its calls to another Context और ContextThemeWrapperallows you to modify the theme from what is in the wrapped context है।

हालांकि, सवाल यह है कि यह वास्तव में क्यों हो रहा है (जैसा कि स्पष्ट था, के विपरीत) के बारे में अधिक जानकारी थी, यहां कुछ उपयोगी पोस्ट भी हैं जो गलत तरीके से आवेदन संदर्भ का उपयोग करके और सही तरीके से संदर्भ का चयन कैसे करें:

अधिकांश @CommonsWare से महत्वपूर्ण बात: "getApplicationContext() नहीं एक है पूर्ण संदर्भ और परिणामस्वरूप गतिविधि की हर चीज का समर्थन नहीं करता है। " प्रसंग के बारे में

बहुत बढ़िया पोस्ट है कि सब कुछ स्पष्ट करना चाहिए:

+0

मैंने अपना जवाब अपडेट किया। –

+0

महान उत्तर, इसे पूरी तरह से खींचा। उस –

+0

के लिए धन्यवाद अच्छा जवाब +1 .. – Shiv

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