在另一个布局中以编程方式添加布局

17

我有一个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中?


1
你是在 for 循环内部调用 addView 吗? - ρяσѕρєя K
1个回答

20

inflate(int resource, ViewGroup root) 方法会在 root 不为空的情况下返回 root,因此 to_add.findViewById() 等同于 options_layout.findViewById(),并且它总是返回第一个位置的视图。进行以下更改应该有所帮助:

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

}

至:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接