简体   繁体   English

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

[英]Android dynamically add custom component to LinearLayout

i want add a custom component on a LinearLayout whenever i touch a button. 我想在我每次触摸按钮时在LinearLayout上添加自定义组件。

This is my code: 这是我的代码:

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

In this way, only first custom component is added, why? 这样,仅添加第一个自定义组件,为什么?

Thanks 谢谢

edit: I've modified my code: 编辑:我已经修改了我的代码:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
((ViewGroup) root).addView(custom, myCount, params);

This is my custom component: 这是我的自定义组件:

<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>

The first time that i press the button, custom component was added, but all other times NOT. 我第一次按下按钮时,添加了自定义组件,但其他所有时间都没有。 The first custom element is not overwrite, it's the only visible! 第一个自定义元素不会被覆盖,它是唯一可见的元素!

Try this. 尝试这个。 It will help you. 它会帮助你。

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

Where's your layout_cartelle_immagini declared? 您的layout_cartelle_immagini声明在哪里?

Probably it's a horizontal LinearLayout, just set orientation to vertical and your code should work (considering the code to add a custom view is inside a onClickListener). 可能是水平的LinearLayout,只需将方向设置为垂直即可,您的代码应该可以工作(考虑在onClickListener内部添加自定义视图的代码)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM