2011-10-28 10 views
5

मुझे एक सरल स्वाइप नमूना मिला जो मैंने बदल दिया ताकि मुझे नए एक्सएमएल लेआउट बनाने और उन्हें प्रदर्शित करने के लिए मुख्य लेआउट को फुलाया जा सके। मैं जो करना चाहता था वह भी स्वाइप प्रक्रिया के लिए प्रोग्रामेटिक रूप से लेआउट जोड़ने में सक्षम था।प्रोग्रामेटिक रूप से देखने के लिए सबसे अच्छा तरीका

मेरे पास main.xml लेआउट और एक लाल.एक्सएमएल और पीले.एक्सएमएल है जो एक ठोस रंग में एक टेक्स्टव्यू सेट के साथ एक सरल रैखिकआउट है।

नीचे दिया गया कोड काम करता है लेकिन मुझे नहीं लगता कि यह सही है या मैं जो करने की कोशिश कर रहा हूं उसे करने का सबसे अच्छा तरीका है। यदि कोई बेहतर तरीका सुझा सकता है जिसकी सराहना की जाएगी।

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    //Create a layout with a solid blue background programmatically 
    TextView tv1 = new TextView(this); 
    tv1.setText("Blue"); 
    tv1.setBackgroundColor(Color.BLUE); 
    tv1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

    LinearLayout ll = new LinearLayout(this); 
    ll.setOrientation(LinearLayout.VERTICAL); 
    ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    ll.addView(tv1); 
    //Create a layout with a solid green background programmatically 
    TextView tv2 = new TextView(this); 
    tv2.setText("Green"); 
    tv2.setBackgroundColor(Color.GREEN); 
    tv2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

    LinearLayout ll2 = new LinearLayout(this); 
    ll2.setOrientation(LinearLayout.VERTICAL); 
    ll2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    ll2.addView(tv2); 
    //inflate the flipper view and add the yellow and red xml layouts and also the 2 programmatically created layouts 
    fSpace = (ViewFlipper)findViewById(R.id.flipper); 
    inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    inflater.inflate(R.layout.yellow, fSpace); 
    inflater.inflate(R.layout.red, fSpace); 
    fSpace.addView(ll); 
    fSpace.addView(ll2); 

} 

उत्तर

2

जिस तरह से आप R.layout.yellow और R.layout.red को फुलाते हैं, वास्तव में ऐसा करने का सही तरीका है। आप इसे बहुत से एक्सएमएल पर ले जाकर अपने कोड को सरल बना सकते हैं। मुझे लगता है कि टीवी 1 सिर्फ एक नमूना है? यदि नहीं, तो यह main.xml में जा सकता है। आप एक ही मुद्रास्फीति के साथ पीले और लाल बनाने का एक तरीका भी ढूंढ सकते हैं ... आप जो कर रहे हैं उसके आधार पर।

प्रोग्रामेटिक रूप से विचारों को बनाना अधिकांश भाग के लिए, थोड़ा कठिन है।

4

यदि आपके पास एक जटिल लेआउट है जिसे आप प्रोग्रामेटिक रूप से बनाना चाहते हैं, तो लेआउट प्रीमियर xml में सबसे आसान हो सकता है और फिर इसे बढ़ाएं और इसे रनटाइम पर जोड़ें।

एक्सएमएल

यहां नमूने के पूर्व बनाया XML लेआउट लेआउट फ़ोल्डर में है वह यह है कि में दृश्य तैयार करें। आपका कुछ भी हो सकता है, एक दृश्य या एक संपूर्ण जटिल लेआउट।

लेआउट/my_view.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/LinearLayout1" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/TextView1" 
     android:text="This is a TV"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/TextView2" 
     android:text="How are you today?"/> 
</LinearLayout> 

आपके विचार

कुछ जगह यह अपनी गतिविधि लेआउट में आपके विचार रखा है के लिए एक कंटेनर करें। आप इस तरह कुछ हो सकता है।

<FrameLayout 
    android:id="@+id/flContainer" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

</FrameLayout> 

दृश्य

उपयोग फुलाना कंटेनर के लिए एक संदर्भ मिलता है, एक्सएमएल से अपने दृष्टिकोण को बढ़ा देते हैं, और फिर कंटेनर में जोड़ें।

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

    FrameLayout container = (FrameLayout) findViewById(R.id.flContainer); 
    View inflatedLayout= getLayoutInflater().inflate(R.layout.my_view, null, false); 
    container.addView(inflatedLayout); 

} 

ऐसा करने से आपका कोड बहुत साफ हो जाता है।

यह भी देखें:

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