Java Android动态向LinearLayout添加自定义组件

Java Android动态向LinearLayout添加自定义组件,java,android,android-layout,android-linearlayout,Java,Android,Android Layout,Android Linearlayout,每当我按下按钮时,我希望在线性布局上添加自定义组件 这是我的代码: LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); LinearLayout root = (LinearLayout) findViewById(R.id.layout_cartelle_immagini); View custom = vi.inflat

每当我按下按钮时,我希望在线性布局上添加自定义组件

这是我的代码:

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout root = (LinearLayout) findViewById(R.id.layout_cartelle_immagini);

View custom = vi.inflate(R.layout.custom_list_folder, null);
TextView textView = (TextView) custom.findViewById(R.id.label_pathFolder);
textView.setText(pathDirectory);

Spinner spinnerLevel = (Spinner) custom.findViewById(R.id.spinner_level);

try{

root.addView(custom);
}catch(Throwable e) {
    Log.e("BurgerClub", "MEX: " + e.getMessage());
    e.printStackTrace();
}
这样,只添加了第一个自定义组件,为什么

谢谢

编辑: 我修改了我的代码:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
((ViewGroup) root).addView(custom, myCount, params);
这是我的自定义组件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/border_bottom"
    android:layout_marginBottom="2dp"
    android:paddingRight="20dp"
    android:paddingLeft="20dp"
    style="@style/Theme.BurgerClubStyle" >

    <TextView
        android:id="@+id/label_pathFolder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:gravity="center"
        android:padding="20dp"
        android:textSize="25sp"
        style="@style/customText" />

    <Spinner
        android:id="@+id/spinner_level"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/btn_show_desc_img"
        android:entries="@array/n_level_array"
        android:padding="20dp"
        android:prompt="@string/n_level_prompt"
        android:textAlignment="3"
        style="@style/customText" />

    <ImageButton
        android:id="@+id/btn_show_desc_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/btn_remove_folder"
        android:layout_marginLeft="20dp"
        android:layout_centerVertical="true"
        android:background="@drawable/button_press"
        android:contentDescription="@string/showDescImg"
        android:src="@drawable/ic_desc" />

    <ImageButton
        android:id="@+id/btn_remove_folder"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@drawable/button_press"
        android:contentDescription="@string/showDescImg"
        android:src="@drawable/ic_delete" />

</RelativeLayout>

我第一次按下按钮时,添加了自定义组件,但所有其他时间都没有。
第一个自定义元素不是覆盖,它是唯一可见的

试试这个。它会帮助你的

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
root.addContentView(custom, params);

你的布局在哪里?卡特尔·伊马吉尼宣称


可能是水平线性布局,只需将方向设置为垂直,您的代码就可以工作(考虑到添加自定义视图的代码在onclick侦听器中)。

您能更好地描述您面临的问题吗?“只添加第一个组件”是什么意思?您添加了更多组件,但它们重叠。。使用布局参数和规则使新的布局显示在另一个布局的下方,有问题和答案请尝试Googling验证布局的方向我发布的代码位于一个函数中,每当按下“指定”按钮时,该函数都会被调用。我第一次按下按钮时,自定义组件已添加,但所有其他时间均未添加@Sajmon我试过你的代码,但我也有同样的问题,我必须删除视图中的组件,并在添加新的自定义组件时重新插入所有组件吗?@Papsurf我刚刚编辑了答案,这不是我的代码。您需要提供更多信息。