hinzufügen Layout programmgesteuert in ein anderes ein

Ich habe eine xml-Datei (option_element.xml) , enthält eine Bildansicht und eine TextView

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="-18dp" >

    <ImageView
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@drawable/option_button" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button"
        android:layout_alignLeft="@+id/button"
        android:layout_alignRight="@+id/button"
        android:layout_alignTop="@+id/button"
        android:layout_centerHorizontal="true"
        android:gravity="center" />

</RelativeLayout>

Sollte ich füllen Sie ein LinearLayout mit dieser Ansicht, basierend auf einem Inhalt von einem array

<?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="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/options_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" />

    <!-- other layout elements -->

</LinearLayout>

Ich bin das hinzufügen es wie

LinearLayout options_layout = (LinearLayout) findViewById(R.id.options_list);
String[] options = getActivity().getResources().getStringArray(R.array.options);
for (int i = 0; i < options.length; i++) {
    View to_add = inflater.inflate(R.layout.options_element,
                options_layout);

    TextView text = (TextView) to_add.findViewById(R.id.text);
    text.setText(options[i]);
    text.setTypeface(FontSelector.getBold(getActivity()));

}

Aber etwas schief gegangen ist. Ich erwarte Optionen.Länge Bildansicht mit relativ TextView gefüllt mit Optionen[i] text, aber ich erhalten Optionen.Länge Bildansicht, aber nur der erste text (und text es ist nicht in Optionen[0] - element, aber letzten).

Beispielsweise, wenn die option enthält {"eins", "zwei", "drei"} innerhalb der ersten Bildansicht bekomme ich "drei", und andere sind leer.
Wie kann jeder string innerhalb der einzelnen TextView?

Rufen Sie addView im inneren for-Schleife?

InformationsquelleAutor giozh | 2015-02-12

Schreibe einen Kommentar