2012-07-31 13 views
9

हाय सब मेरे पास xml में एक खाली रैखिक लेआउट है। और मैं इसे गतिशील रूप से कुछ पाठ दृश्य जोड़ रहा हूं।रैखिक लेआउट के अंदर रैखिक लेआउट जोड़ रहा है लेकिन दिखाई नहीं दे रहा है

एक बार ये टेक्स्टव्यू 5 से अधिक हो जाने के बाद मैं इसके अंदर एक और रैखिक लेआउट बना रहा हूं और नए बनाए गए लेआउट में टेक्स्ट दृश्य जोड़ रहा हूं। और अंत में मुख्य लेआउट में यह नया लेआउट जोड़ना।

मैं जोड़ने में सक्षम हूं, यानी एमुलेटर में यह उस स्थान पर कब्जा कर रहा है लेकिन टेक्स्टव्यू में जानकारी प्रदर्शित नहीं करेगा।

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

    <ScrollView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
      android:id="@+id/dyn_layout" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:padding="10dip" > 
     </LinearLayout> 
    </ScrollView> 

    <Button 
     android:id="@+id/dyn_button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Add TextViews" /> 

</LinearLayout> 

और मेरे जावा फ़ाइल इस प्रकार है::

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.TextView; 

public class AddTextViewsDynamically extends Activity implements 
     OnClickListener { 

    Button button; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.add_textview_dynamically); 

     button = (Button) findViewById(R.id.dyn_button1); 
     button.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.dyn_button1: 
      LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout); 
      LinearLayout layout2 = null; 
      LinearLayout layout3 = null; 
      for (int i = 0; i < 10; i++) { 
       if (i > 4) { 
        if (i == 5) { 
         layout2 = new LinearLayout(this); 
         layout2.setLayoutParams(new LayoutParams(
           LayoutParams.FILL_PARENT, 
           LayoutParams.WRAP_CONTENT)); 
         layout2.setOrientation(LinearLayout.VERTICAL); 
         layout2.setPadding(10, 60, 10, 10); 
         layout3 = new LinearLayout(this); 
         layout3.setLayoutParams(new LayoutParams(
           LayoutParams.FILL_PARENT, 
           LayoutParams.WRAP_CONTENT)); 
         layout3.setOrientation(LinearLayout.HORIZONTAL); 
         layout3.setPadding(10, 10, 10, 10); 
        } 
        System.out.println("**** Adding text view " + i); 
        TextView text = new TextView(this); 
        text.setText("The Value of i is :" + i); 
        text.setTextSize(12); 
        text.setGravity(Gravity.LEFT); 
        text.setLayoutParams(new LayoutParams(155, 
          LayoutParams.WRAP_CONTENT)); 
        layout3.addView(text); 

        if (i == 9) { 
         System.out 
           .println("Added second linear layout to first"); 
         layout2.setVisibility(View.VISIBLE); 
         layout2.addView(layout3); 
         layout.addView(layout2); 
        } 
       } else { 
        System.out.println("###### Adding text view " + i); 
        TextView text = new TextView(this); 
        text.setText("The Value of i is :" + i); 
        text.setTextSize(12); 
        text.setGravity(Gravity.LEFT); 
        text.setLayoutParams(new LayoutParams(155, 
          LayoutParams.WRAP_CONTENT)); 
        layout.addView(text); 
       } 
      } 

     } 

    } 

} 

किसी भी एक टेलीफोन मुझे मैं कहाँ गलत जा रहा हूँ कर सकते हैं

मेरी एक्सएमएल इस प्रकार है। और कृपया मेरी मदद करो।

अग्रिम Mouni

+0

आप बता सकते हैं कि तुम ऐसा क्यों कर रहे हैं ताकि वैकल्पिक और सबसे अच्छा समाधान उपलब्ध कराया जा सकता है तो क्या होगा? –

+0

मुझे गतिशील रूप से सेवा से कुछ जानकारी मिल जाएगी जो मुझे प्रत्येक पंक्ति में पाठ दृश्य 5 में प्रदर्शित करना है। यदि अगली पंक्ति में यह और अधिक है। – Mouni

+0

क्या कोई अन्य तरीका है जिससे मैं इसे कर सकता हूं .. ?? कृपया मुझे बताएं – Mouni

उत्तर

5

आपका प्राथमिक LinearLayout क्षैतिज करने के लिए सेट तो पहले 5 पाठ दृश्य और layout2 एक ही लाइन पर दिखाया जाता है है। लेआउट 3 में लेआउट 3 जोड़ना प्राथमिक रैखिक लेआउट से अंतिम पाठ दृश्य के दाईं ओर लेआउट 3 दिखाया गया है। 10 इंच टैबलेट पर मैं केवल आपके लीनियरलाउट के पहले 2 तत्व देखता हूं। शायद एक छोटी सी स्क्रीन पर आप उन्हें नहीं देखते हैं। का उपयोग कर

text.setLayoutParams(new LayoutParams(50, LayoutParams.WRAP_CONTENT)); 

text.setLayoutParams(new LayoutParams(155, LaoutParams.WRAP_CONTENT)); 

के बजाय की कोशिश करो और आप अपने सभी पाठ विचारों को देखना चाहिए।

संपादित करें: आपके मामले में यह चाल चलनी चाहिए; xml:

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

    <ScrollView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
      android:id="@+id/dyn_layout" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:padding="10dip" > 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/dyn_layout2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:visibility="gone" 
      android:padding="10dip" > 
     </LinearLayout> 
    </ScrollView> 

    <Button 
     android:id="@+id/dyn_button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Add TextViews" /> 

</LinearLayout> 

और कोड:

@Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
     case R.id.dyn_button1: 
      LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout); 
      LinearLayout layout2 = (LinearLayout) findViewById(R.id.dyn_layout2); 
      LinearLayout layout3 = null; 
      for (int i = 0; i < 10; i++) { 
       if (i > 4) { 
        if (i == 5) { 
         layout2.setPadding(10, 60, 10, 10); 
         layout3 = new LinearLayout(this); 
         layout3.setLayoutParams(new LayoutParams(
           LayoutParams.FILL_PARENT, 
           LayoutParams.WRAP_CONTENT)); 
         layout3.setOrientation(LinearLayout.HORIZONTAL); 
         layout3.setPadding(10, 10, 10, 10); 
        } 
        System.out.println("**** Adding text view " + i); 
        TextView text = new TextView(this); 
        text.setText("The Value of i is :" + i); 
        text.setTextSize(12); 
        text.setGravity(Gravity.LEFT); 
        text.setLayoutParams(new LayoutParams(155, 
          LayoutParams.WRAP_CONTENT)); 
        layout3.addView(text); 

        if (i == 9) { 
         System.out 
           .println("Added second linear layout to first"); 
         layout2.setVisibility(View.VISIBLE); 
         layout2.addView(layout3); 
        } 
       } else { 
        System.out.println("###### Adding text view " + i); 
        TextView text = new TextView(this); 
        text.setText("The Value of i is :" + i); 
        text.setTextSize(12); 
        text.setGravity(Gravity.LEFT); 
        text.setLayoutParams(new LayoutParams(155, 
          LayoutParams.WRAP_CONTENT)); 
        layout.addView(text); 
       } 
      } 

     } 

    } 
+0

आपको बहुत बहुत धन्यवाद। हाँ सभी 10 टेक्स्टव्यू लंबवत जोड़ रहे हैं। लेकिन मुझे एक पंक्ति में केवल 5 चाहिए और अगले 5 में मुझे लगता है कि इस तरह से लेआउट प्राप्त करने के लिए मुझे क्या करना है ..? कृपया मेरी मदद करें .. – Mouni

+0

हाँ, यह काम करता है लेकिन छोटे बदलावों के साथ मैं xml में लेआउट नहीं जोड़ता .. यह गतिशील रूप से जोड़ा जाएगा .. अब यह ठीक काम कर रहा है .. :) एक बार फिर धन्यवाद .. – Mouni

0

में धन्यवाद यह एक प्रयास करें, तो आप उस अपने कोड ठीक है देखेंगे।

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

<HorizontalScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_weight="80" > 

    <LinearLayout 
     android:id="@+id/dyn_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:padding="10dip" > 
    </LinearLayout> 
</HorizontalScrollView> 

<Button 
    android:id="@+id/dyn_button1" 
    android:layout_width="wrap_content" 
    android:layout_height="0dp" 
    android:layout_weight="20" 
    android:text="Add TextViews" /> 

</LinearLayout> 
+0

नोप डिंट ने काम किया .. :(स्क्रीन के अंत में बस बटन आ रहा है ..क्या कुछ ऐसा है जो मुझे बदलना है .. ?? – Mouni

+0

मेरा संपादित उत्तर देखें, यदि आप क्षैतिज रूप से टेक्स्ट दृश्य जोड़ना चाहते हैं तो आपको नियमित रूप से क्षैतिज स्क्रॉलव्यू का उपयोग करने की आवश्यकता है। और अगर यह काम करता है तो मुझे बताएं, (जिस तरह से मैंने कोशिश की, और यह मेरे साथ काम किया) – osayilgan

+0

उत्तर देने के लिए धन्यवाद .. अब भी कोई भी पहले जैसा नहीं बदलता .. – Mouni

0

असल में आप सही काम कर रहे हैं, मैंने सिर्फ पैडिंग हटा दी है और सभी रैखिक लेआउट को लंबवत करने के लिए उन्मुखीकरण किया है। इसे एक्सएमएल फाइल पर भी संशोधित करें और यह काम करेगा :))) यह सिर्फ आप 10 या 60 का पैडिंग जोड़ रहे हैं जो माता-पिता द्वारा किए गए दृश्य से जा रहा है।

संशोधित जावा कोड:

@Override 
public void onClick(View v) { 

switch (v.getId()) { 
    case R.id.dyn_button1: 
     LinearLayout layout = (LinearLayout) findViewById(R.id.dyn_layout); 
     LinearLayout layout2 = null; 
     LinearLayout layout3 = null; 
     for (int i = 0; i < 10; i++) { 
      if (i > 4) { 
       if (i == 5) { 
        layout2 = new LinearLayout(this); 
        layout2.setLayoutParams(new LayoutParams(
          LayoutParams.FILL_PARENT, 
          LayoutParams.WRAP_CONTENT)); 
        layout2.setOrientation(LinearLayout.VERTICAL); 
       // layout2.setPadding(10, 10, 10, 10); 
        layout3 = new LinearLayout(this); 
        layout3.setLayoutParams(new LayoutParams(
          LayoutParams.FILL_PARENT, 
          LayoutParams.WRAP_CONTENT)); 
        layout3.setOrientation(LinearLayout.VERTICAL); 
       //  layout3.setPadding(10, 10, 10, 10); 
       } 
       System.out.println("**** Adding text view " + i); 
       TextView text = new TextView(this); 
       text.setText("The Value of i is :" + i); 
       text.setTextSize(12); 
       text.setGravity(Gravity.LEFT); 
       text.setLayoutParams(new LayoutParams(155, 
         LayoutParams.WRAP_CONTENT)); 
       layout3.addView(text); 

       if (i == 9) { 
        System.out 
          .println("Added second linear layout to first"); 
        layout2.setVisibility(View.VISIBLE); 
        layout2.addView(layout3); 
        layout.addView(layout2); 
       } 
      } else { 
       System.out.println("###### Adding text view " + i); 
       TextView text = new TextView(this); 
       text.setText("The Value of i is :" + i); 
       text.setTextSize(12); 
       text.setGravity(Gravity.LEFT); 
       text.setLayoutParams(new LayoutParams(155, 
         LayoutParams.WRAP_CONTENT)); 
       layout.addView(text); 


      } 
     } 

    } 

} 
+0

हैलो अरे, क्या आपको दो पंक्तियां मिल रही हैं या सिर्फ एक..? – Mouni

+0

हाँ यह लंबवत लेआउट के साथ ठीक काम करता है .. लेकिन मैं इसे क्षैतिज लेआउट यानी प्रत्येक पंक्ति में 5 मान चाहता हूं .. मुझे इसके लिए क्या करना चाहिए .. ?? – Mouni

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