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
Du musst angemeldet sein, um einen Kommentar abzugeben.
Den Weg, dass Sie das hinzufügen Dinge auf die Ansicht ist im Grunde Recht - Sie bekommen nur das container-Objekt anhand der ID und fügen Sie Sie dann.
Sieht es aus wie Sie den Inhalt anzeigen, um die falsche Sicht obwohl. Befüllen Sie die Ansicht, Kinder und legen Sie es als die Ansicht "Inhalt", oder der Inhalts-Ansicht von einer Ressourcen-ID, dann tun die manipulation. Was Sie tun, ist die Manipulation einer Ansicht und hinzufügen von einem anderen. Versuchen Sie, Ihre setContentView(...) zu Beginn des Blocks.
InformationsquelleAutor der Antwort fleetway76