Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/211.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在片段内以编程方式将视图添加到另一个视图_Android_Android Fragments_Android Linearlayout - Fatal编程技术网

Android 在片段内以编程方式将视图添加到另一个视图

Android 在片段内以编程方式将视图添加到另一个视图,android,android-fragments,android-linearlayout,Android,Android Fragments,Android Linearlayout,我的代码出了问题。我有一个表示片段布局的XML(fragment_layout.XML): 以及另一个xml文件,表示应该放在片段布局中的单个视图(element.xml): 这就是我在onCreateView方法中填充片段布局的方式: View view = inflater.inflate(R.layout.fragment_layout, container, false); some_elements = getActivity().getRes

我的代码出了问题。我有一个表示片段布局的XML(fragment_layout.XML):


以及另一个xml文件,表示应该放在片段布局中的单个视图(element.xml):


这就是我在onCreateView方法中填充片段布局的方式:

View view = inflater.inflate(R.layout.fragment_layout, container,
            false);

    some_elements = getActivity().getResources().getStringArray(
            R.array.some_array);

    LinearLayout root = (LinearLayout) view.findViewById(R.id.root_layout);

    for (int i = 0; i < some_elements.length; i++) {

        View element = inflater.inflate(
                R.layout.element, container, false);

        TextView tv= (TextView) element.findViewById(R.id.tv);
        prodotto.setText(prodotti[i]);

        EditText et= (EditText) element.findViewById(R.id.et);

        root.addView(element);

    }

    return view; 
视图=充气机。充气(R.layout.fragment_布局,容器,
假);
某些元素=getActivity().getResources().getStringArray(
R.数组。一些数组);
LinearLayout root=(LinearLayout)view.findViewById(R.id.root\u布局);
for(int i=0;i

现在,片段中只显示“some_elements”数组的第一个元素(元素包含10个元素)。为什么没有显示其他元素?出什么问题了?

问题在于您的RelativeLayout中的android:layout\u height=“match\u parent”
。应该是
wrap\u content

您也可以在滚动视图中包装您的根目录布局。我的错误,非常感谢。工作太多了,对我来说,幸运的是今天是星期五!
<?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" >

<TextView
    android:id="@+id/tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true" />

<EditText
    android:id="@+id/et"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:inputType="number"
    android:text="0" />

</RelativeLayout>   
View view = inflater.inflate(R.layout.fragment_layout, container,
            false);

    some_elements = getActivity().getResources().getStringArray(
            R.array.some_array);

    LinearLayout root = (LinearLayout) view.findViewById(R.id.root_layout);

    for (int i = 0; i < some_elements.length; i++) {

        View element = inflater.inflate(
                R.layout.element, container, false);

        TextView tv= (TextView) element.findViewById(R.id.tv);
        prodotto.setText(prodotti[i]);

        EditText et= (EditText) element.findViewById(R.id.et);

        root.addView(element);

    }

    return view;