Ho un file XML (option_element.xml) che contiene un ImageView e un TextViewaggiungere layout di programmazione all'interno di un altro
<?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>
devo compilare un LinearLayout con questa visione, sulla base di un contenuto di un 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>
sto aggiungendo che come
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()));
}
Ma qualcosa andato storto. Mi aspetto options.length ImageView con TextView relativo riempito con opzioni [i] testo, ma ottengo options.length ImageView, ma solo il primo ha testo (e testo non è l'elemento [0] opzioni, ma l'ultimo).
Ad esempio, se l'opzione contiene {"uno", "due", "tre"} all'interno di prima imageView ottengo "tre" e altri sono vuoti. Come si può inserire ogni stringa all'interno di ogni TextView?
stai chiamando 'addView' dentro per il ciclo? –