2012-07-31 9 views
9

Hallo allerseits, ich habe ein leeres lineares Layout in Xml. Und ich füge dynamisch eine Textansicht hinzu.lineares Layout innerhalb des linearen Layouts fügt hinzu, ist aber nicht sichtbar

Sobald diese Textansichten 5 überschreiten, erstelle ich ein weiteres lineares Layout darin und füge dem neu erstellten Layout Textansichten hinzu. Und schließlich dieses neue Layout zum Hauptlayout hinzufügen.

Ich bin in der Lage, hinzuzufügen, d. H. Im Emulator besetzen diesen Raum aber, wird nicht angezeigt, die Informationen in den Textansichten.

mein xml ist wie folgt:

<?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> 

und meine Java-Datei ist wie folgt:

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

     } 

    } 

} 

Kann man tel mir, wo ich falsch gehe. Und bitte hilf mir.

Vielen Dank im Voraus Mouni

+0

Können Sie erklären, warum Sie dies tun, so dass alternative und besten Lösungen, wenn zur Verfügung gestellt werden kann? –

+0

Ich werde dynamisch einige Informationen vom Dienst bekommen, die ich in Textansichten 5 in jeder Zeile anzeigen muss. wenn es mehr als in der nächsten Zeile ist. – Mouni

+0

Gibt es einen anderen Weg, von dem ich es tun kann .. ?? Bitte lassen Sie mich wissen – Mouni

Antwort

5

Ihre primäre Linearlayout wird auf Horizontal gesetzt, so dass die ersten 5 Textansicht und die Layout2 auf der gleichen Linie dargestellt sind. Durch Hinzufügen von Layout3 zu Layout2 wird das Layout3 rechts von der letzten Textansicht des primären linearen Layouts angezeigt. Auf einem 10-Zoll-Tablet sehe ich nur die ersten 2 Elemente Ihres LinearLayout. Vielleicht sehen Sie sie auf einem kleineren Bildschirm nicht. Versuchen

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

statt

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

verwenden und Sie sollten alle Ihre Textansichten sehen.

EDIT: In Ihrem Fall sollte dies den Trick tun; 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> 

und Code:

@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

Vielen Dank so viel. Ja, alle 10 Textansichten werden vertikal hinzugefügt. Aber ich möchte nur 5 in einer Zeile und weitere 5 in der nächsten, was soll ich tun, um das Layout auf diese Weise zu bekommen ..? plz helfe mir aus .. – Mouni

+0

yeah es funktionierte aber mit kleinen Änderungen, da ich nicht Layout in XML hinzufügen .. es wird dynamisch hinzugefügt .. Jetzt funktioniert es gut .. :) Vielen Dank noch einmal .. – Mouni

0

versuchen diese, dann werden Sie sehen, dass der Code in Ordnung ist.

<?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

Nö dint ausgearbeitet .. :(Das diff ist nur Knopf kommt am Ende des Bildschirms ..Gibt es etwas, das ich ändern muss? – Mouni

+0

sehe meine bearbeitete Antwort, wenn Sie die Textansicht horizontal hinzufügen möchten, müssen Sie HorizontalScrollView anstelle von regulären verwenden. Und lass es mich wissen, wenn es funktioniert, (übrigens versuchte ich es, und es hat mit mir funktioniert) – osayilgan

+0

danke für die Antwort .. Auch jetzt keine Änderung ist das gleiche wie zuvor .. – Mouni

0

Eigentlich machst du Dinge richtig, ich habe nur Padding entfernt und die Ausrichtung aller linearen Layouts auf vertikal gemacht. Ändern Sie dies auch in der XML-Datei und es wird funktionieren :))) Es ist nur Sie fügen eine Auffüllung von 10 oder 60, die von der Ansicht von der übergeordneten genommen wird.

Modified Java-Code:

@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

Hallo abhy, erhalten Sie zwei Zeilen von Informationen oder einfach ein..? – Mouni

+0

Ja, es funktionierte gut mit vertikalem Layout .. Aber ich möchte es in horizontalem Layout, dh 5 Werte in jeder Zeile .. was sollte ich dafür tun .. ?? – Mouni

Verwandte Themen