Wie füge ich Elemente dynamisch zu einer mit XML erstellten Ansicht hinzu?

Ich versuche, fügen Sie dynamische Inhalte zu einer Ansicht, die erstellt wurde, mit XML.

Ich habe eine view "profile_list", die eine ScrollView, die ich möchte hinzufügen, dass einige Elemente zu.

Hier ist etwas code, der das, was ich versuche zu tun.

//Find the ScrollView 
ScrollView sv = (ScrollView) this.findViewById(R.id.scrollView1);

//Create a LinearLayout element
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

//Add text
tv = new TextView(this);
tv.setText("my text");
ll.addView(tv);

//Add the LinearLayout element to the ScrollView
sv.addView(ll);

//Display the view
setContentView(R.layout.profile_list);

Der plan ist, fügen Sie ein TableLayout und füllen Sie es dynamisch und nicht nur ein dummy-text, aber erstmal muss ich diese zu arbeiten.

Jede Hilfe ist willkommen.

Freundlichen GRÜßEN
Olle

Ich die Lösung gefunden!

Dumm mich, ich hatte Links ein element in meinem ScrollView in meiner XML-Datei!

Sowieso Ihr ist ein funktionierendes Beispiel:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.profile_list, null);

    //Find the ScrollView 
    ScrollView sv = (ScrollView) v.findViewById(R.id.scrollView1);

    //Create a LinearLayout element
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);

    //Add text
    TextView tv = new TextView(this);
    tv.setText("my text");
    ll.addView(tv);

    //Add the LinearLayout element to the ScrollView
    sv.addView(ll);

    //Display the view
    setContentView(v);

Und die XML-Datei entsprechend

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

    <ScrollView 
        android:id="@+id/scrollView1" 
        android:clickable="true" 
        android:layout_weight="1" 
        android:layout_width="fill_parent" 
        android:layout_marginBottom="50px" 
        android:layout_height="fill_parent">
    </ScrollView>

</LinearLayout>

Hoffe, dies hilft jemand. Vielen Dank für alle Hilfe!

InformationsquelleAutor der Frage Olle | 2011-06-16

Schreibe einen Kommentar