6

Mam plik XML (option_element.xml), który zawiera ImageView i TextViewdodać układ programowo wewnątrz innego jednej

<?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> 

powinien zapełnić się LinearLayout z tym poglądem, w oparciu o treść tablicy

<?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> 

Dodaję go jak

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())); 

} 

Ale coś poszło nie tak. Spodziewam się options.length ImageView z względnym TextView wypełnionym opcjami [i] text, ale otrzymuję options.length ImageView, ale tylko pierwszy ma tekst (i tekst to element nie [0] opcji, ale ostatni).

Na przykład, jeśli opcja zawiera {"jeden", "dwa", "trzy"} w pierwszym obrazie Wyświetlam, otrzymuję "trzy", a pozostałe są puste. W jaki sposób umieścić każdy ciąg w każdym widoku tekstowym?

+1

Czy wywołanie 'addView' wewnątrz pętli for? –

Odpowiedz

6

Zwrot inflate(int resource, ViewGroup root) metoda rootroot jeśli nie jest pusty, więc to_add.findViewById() równa options_layout.findViewById() i zawsze powrócić widoków na pierwszej pozycji. Zmiana w następujący sposób powinno pomóc:

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())); 

} 

do:

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,false); 

    TextView text = (TextView) to_add.findViewById(R.id.text); 
    text.setText(options[i]); 
    text.setTypeface(FontSelector.getBold(getActivity())); 
    options_layout.addView(to_add); 
} 
Powiązane problemy