add Layout programmatically inside another one

纵饮孤独 提交于 2020-02-19 10:08:49

问题


I have an xml file (option_element.xml) that contains a ImageView and a 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>

I should fill a LinearLayout with this View, based on a content of an 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>

I'm adding it like

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

}

But something gone wrong. I'm expecting options.length ImageView with relative TextView filled with options[i] text, but i obtain options.length ImageView, but only first one has text (and text it's not options[0] element, but last one).

For example, if option contains {"one", "two", "three"} inside first imageView i get "three", and other ones are empty. How can put each string inside each TextView?


回答1:


The inflate(int resource, ViewGroup root) method return root if root is not null, thus to_add.findViewById() equals options_layout.findViewById() and it always return the Views in the first position. Change as following should help:

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

}

to:

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


来源:https://stackoverflow.com/questions/28473145/add-layout-programmatically-inside-another-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!