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


79

यदि उदाहरण के लिए मैंने एक जड़ रैखिक लेआउट को परिभाषित किया है जिसका अभिविन्यास लंबवत है:

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>

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

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

जवाबों:


102

अपने में 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एस। वे आसानी से प्रोग्राम बनाया है।


5
किसी को पता है क्यों है यह? यदि मैं LinearLayout का विस्तार करता है तो मैं इसे सिर्फ क्यों नहीं कर सकता (देखें 1)?
मेगाविजेट

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

हमें गतिशील हॉर्नस्कूलर व्यू में भी रैखिक लेआउट जोड़ना होगा। tnx
:)

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

1
अगर हम ऐसा करते हैं। हम "layout.child" के अंदर क्लिक घटनाओं को संभालने का प्रबंधन कैसे करते हैं?
मलिंडू

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

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);

0

मुझे कोटलिन में रैखिक लेआउट जैसे विचारों को जोड़ने का अधिक सटीक तरीका मिला (इनफ्लो में पास पैरेंट लेआउट) (और गलत)

val parentLayout = view.findViewById<LinearLayout>(R.id.llRecipientParent)
val childView = layoutInflater.inflate(R.layout.layout_recipient, parentLayout, false)
parentLayout.addView(childView)
हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.