Tôi có một file xml (option_element.xml) có chứa một ImageView và một TextViewthêm Layout lập trình bên trong một một
<?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>
tôi nên điền vào một LinearLayout có tầm nhìn này, dựa trên nội dung của một mảng
<?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>
tôi thêm nó như
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()));
}
Nhưng một cái gì đó đi sai. Tôi mong đợi tùy chọn Image.length ImageView với TextView tương đối chứa các tùy chọn [i] văn bản, nhưng tôi có được ImageView chiều dài tùy chọn, nhưng chỉ có một văn bản đầu tiên (và văn bản nó không phải là phần tử [0], nhưng cuối cùng).
Ví dụ: nếu tùy chọn chứa {"một", "hai", "ba"} bên trong hình ảnh đầu tiên, tôi nhận được "ba" và các tùy chọn khác trống. Làm cách nào để đặt mỗi chuỗi bên trong mỗi TextView?
Bạn đang gọi 'addView' bên trong vòng lặp for? –