以编程方式在另一个内部添加布局 [英] add Layout programmatically inside another one

查看:57
本文介绍了以编程方式在另一个内部添加布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含ImageView和TextView的xml文件(option_element.xml)

 <?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,相对TextView填充有options [i]文本,但我获得options.length ImageView,但只有第一个具有文本(而文本不是options [0]元素,而是最后一个)./p>

例如,如果选项在第一个imageView中包含{"one","two","three"},我将得到"three",而其他则为空. 如何将每个字符串放在每个TextView中?

解决方案

如果root不为null,则inflate(int resource, ViewGroup 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);
}

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?

解决方案

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

这篇关于以编程方式在另一个内部添加布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆