我有一个XML文件(option_element.xml),其中包含一个ImageView和一个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>
根据数组的内容,我应该使用这个View填充一个LinearLayout
<?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>
我像这样添加它
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()));
}
但是出了些问题。我期望有options.length个ImageView,每个ImageView中都有相对应的TextView填充着options[i]文本,但实际上我得到的是options.length个ImageView,只有第一个具有文本(而且文本不是options[0]元素,而是最后一个元素)。
例如,如果选项包含{"one","two","three"},则在第一个imageView中我会得到"three",其他的都是空的。 如何将每个字符串放入每个TextView中?
addView
吗? - ρяσѕρєя K