Android动态地将自定义组件添加到LinearLayout

时间:2013-08-07 15:45:01

标签: java android android-layout android-linearlayout

每当我触摸按钮时,我都希望在LinearLayout上添加自定义组件。

这是我的代码:

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>

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

2 个答案:

答案 0 :(得分:1)

试试这个。它会对你有所帮助。

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

答案 1 :(得分:1)

你的layout_cartelle_immagini在哪里宣布?

可能它是一个水平的LinearLayout,只是将方向设置为垂直,你的代码应该工作(考虑到添加自定义视图的代码在onClickListener中)。