Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.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
Java 将RelativeLayout子级动态添加到父布局不起作用_Java_Android_Android Layout - Fatal编程技术网

Java 将RelativeLayout子级动态添加到父布局不起作用

Java 将RelativeLayout子级动态添加到父布局不起作用,java,android,android-layout,Java,Android,Android Layout,我试图将子视图动态膨胀为父视图,但它抛出了一个错误 我正在尝试将每个RelativeLayout孩子添加到另一个孩子的下方 这是我的密码: 父布局 <RelativeLayout android:id="@+id/work_list" android:layout_width="match_parent" android:layout_height="wrap_content"> </RelativeLayout> &l

我试图将子视图动态膨胀为父视图,但它抛出了一个错误

我正在尝试将每个RelativeLayout孩子添加到另一个孩子的下方

这是我的密码:

父布局

<RelativeLayout
        android:id="@+id/work_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

</RelativeLayout>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff">

    <ImageView
        android:id="@+id/work_pic"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:src="@mipmap/image_placeholder"/>

</RelativeLayout>

我做错了什么?如果我错了,我的代码有什么修正,伙计们?

RelativeLayout child=(RelativeLayout)findViewById(R.id.work\u list)//新子女

事实并非如此
findviewbyd
返回布局中已存在的现有子级。您必须使用其构造函数创建新的
RelativeLayout


RelativeLayout child=新的RelativeLayout(此)

RelativeLayout child=(RelativeLayout)findViewById(R.id.work\u列表)//新子女

事实并非如此
findviewbyd
返回布局中已存在的现有子级。您必须使用其构造函数创建新的
RelativeLayout


RelativeLayout child=新的RelativeLayout(此)

消除了错误,但我现在在视图中看不到子布局。看起来他们没有膨胀:/噢,当然没有,我还没有把布局设置为膨胀:D我该怎么做,伙计?@Earthling你已经接受了答案-你还需要帮助吗?膨胀是使用构造函数创建布局的一种替代方法,所以如果以编程方式创建布局,则无需膨胀布局。我想说,您没有(没有)看到它们,因为它们是空的,并且它们的大小设置为
WRAP\u CONTENT
,消除了错误,但我现在在视图中没有看到子布局。看起来他们没有膨胀:/噢,当然没有,我还没有把布局设置为膨胀:D我该怎么做,伙计?@Earthling你已经接受了答案-你还需要帮助吗?膨胀是使用构造函数创建布局的一种替代方法,所以如果以编程方式创建布局,则无需膨胀布局。我想说你没有看到它们,因为它们是空的,并且它们的大小被设置为
WRAP\u CONTENT
RelativeLayout parent = (RelativeLayout) findViewById(R.id.work_list);

//array containing all children ids
ArrayList<Integer> children = new ArrayList<>();

//adding 10 children to the parent
for(int i=0;i<10;i++) {

    RelativeLayout child = (RelativeLayout) findViewById(R.id.work_list); //new child
    child.setId(i); //setting an id for the child
    children.add(i); //adding the child's id to the list

    if(i!=0) //if it isn't the 1st child, stack them below one another, since the 1st child does not have a child to stack below
    {
                RelativeLayout.LayoutParams params 
                           = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.addRule(RelativeLayout.BELOW, children.get(i - 1)); //stack it below the previous child
                child.setLayoutParams(params);
    }

    parent.addView(child); //add the new child
}
parent.addView(child);