Hallo zusammen,
ich versuche mich grad im Bereich Android und baue an meiner ersten App. Dabei bin ich auch ein (merkwürdiges?) Problem gestoßen. Vielleicht hat von euch ja jemand eine Idee.
Folgendes Layout versuche ich zu realisieren:
layout.xml
[spoiler]
[XML]
<!-- hier ist noch eine box -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent">
<!-- box container -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- box 1 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_dark"
android:layout_marginBottom="10px">
<!-- ein paar weitere Elemente -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test" />
</LinearLayout>
<!-- box 2 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_dark"
android:layout_marginBottom="10px">
<!-- ein paar weitere Elemente -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test" />
</LinearLayout>
<!-- box 3 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_red_dark"
android:layout_marginBottom="10px">
<!-- ein paar weitere Elemente -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test" />
</LinearLayout>
</LinearLayout>
</ScrollView>
[/XML]
[/spoiler]
Kurze Erläuterung: Ganz oben gibt es immer eine Box die ein paar Daten anzeigt. Darunter gibt es dann x-beliebig viele Boxen die man durchscrollen können soll.
Das oben gepostete Layout funktioniert wunderbar.
Wenn ich aber versuche die Boxen 1-3 dynamisch hinzuzufügen (per Schleife im Code und addView) verschwinden die Margins zwischen den roten Boxen. Muss man nach dem adden noch irgendwas aufrufen?
Die dynamische Variante schaut in etwa so aus:
LinearLayout container = (LinearLayout) view.findViewById(R.id.test_container);
for (int i = 0; i < 3; i++) {
View itemView = inflater.inflate(R.layout.item, viewGroup, false);
container.addView(itemView);
}
layout.xml
[spoiler]
[XML]
<Button
android:id="@+id/notification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Test Test" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent">
<!-- container for boxes -->
<LinearLayout
android:id="@+id/test_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
[/XML]
[/spoiler]
item.xml
[spoiler]
[XML]
<!-- ein paar weitere Elemente -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Test" />
[/XML]
[/spoiler]
Ich sehe da keinen Unterschied. Weiß jemand von euch Rat?