ボタンをタッチするたびに 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>
ボタンを初めて押すと、カスタムコンポーネントが追加されましたが、それ以外の場合はそうではありません。最初のカスタム要素は上書きされず、唯一表示されます!