2011-07-12 17 views
57

उदाहरण के लिए मैं एक रूट रेखीय लेआउट जिसका उन्मुखीकरण खड़ी है निर्धारित किया है तो:गतिशील रूप से एक रैखिक लेआउट में सामग्री जोड़ना?

main.xml:

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

    <!-- I would like to add content here dynamically.--> 

</LinearLayout> 

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

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

    <!-- 1st child (1st row)--> 
    <LinearLayout 
     ... 
     android:orientation="horizontal"> 

      <TextView .../> 
      <TextView .../> 
      <TextView .../> 
    </LinearLayout> 

    <!-- 2nd child (2nd row)--> 
    ... 
</LinearLayout> 

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

बच्चे लेआउट जैसे के साथ उदाहरण के जड़ के लिए

दूसरा लेआउट पहले प्रोग्रामेटिक रूप से कैसे जोड़ा जा सकता है, जो प्रत्येक बच्चे के लिए सभी लेआउट विशेषताओं को भी सेट कर सकता है और बच्चे के अंदर अन्य तत्व जोड़ सकता है?

उत्तर

75

अपने onCreate() में, लिखने निम्नलिखित

LinearLayout myRoot = (LinearLayout) findViewById(R.id.my_root); 
LinearLayout a = new LinearLayout(this); 
a.setOrientation(LinearLayout.HORIZONTAL); 
a.addView(view1); 
a.addView(view2); 
a.addView(view3); 
myRoot.addView(a); 

view1, view2 और view3 अपने TextView रों हैं। वे आसानी से प्रोग्रामेटिक रूप से बनाए जाते हैं।

+1

कोई भी जानता है कि यह क्यों है? मैं ऐसा क्यों नहीं कर सकता .addView (देखें 1) यदि यह LinearLayout फैलाता है? – MegaWidget

+0

यह पूरी तरह से जगह से बाहर है, लेकिन मुझे यह अविश्वसनीय रूप से संतोषजनक लगता है कि जब मैं आपके कोड को हाइलाइट करता हूं, तो नीचे 4 पंक्तियां एक ही कॉलम में समाप्त होती हैं। आपने उस कारण से पूरी तरह से अपना उत्थान अर्जित किया है। – RobotKarel314

+0

अच्छा है, लेकिन मुझे इसका उपयोग कब करना चाहिए, और इसके बजाय मुझे टेबललेआउट का उपयोग कब करना चाहिए? क्या यह सिर्फ व्यक्तिगत स्वाद का मामला है? – Zerato

61
LinearLayout layout = (LinearLayout)findViewById(R.id.layout); 
View child = getLayoutInflater().inflate(R.layout.child, null); 
layout.addView(child); 
+1

हम इस तरह करते हैं। हम "layout.child" के अंदर क्लिक ईवेंट को संभालने का प्रबंधन कैसे करते हैं? – Warapitiya

+2

@ वार्पिती तत्व प्राप्त करने और ऑनक्लिक श्रोता सेट करने के लिए 'child.findViewById' का उपयोग करें। – Choxmi

5

आप इस तरह व्यापक LinearLayout प्राप्त कर सकते हैं:

LinearLayout root = (LinearLayout) findViewById(R.id.my_root);  
LinearLayout llay1 = new LinearLayout(this);  
root.addView(llay1); 
LinearLayout llay2 = new LinearLayout(this);  
llay1.addView(llay2); 
संबंधित मुद्दे