2013-08-07 51 views
1

我想在LinearLayout上添加一個自定義組件,只要我觸摸一個按鈕。Android動態添加自定義組件到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> 

的第一次,我按下按鈕,自定義組件被添加,但所有其他時間不是。 第一個自定義元素沒有被覆蓋,它是唯一可見的!

+0

你能描述一下你面臨的問題好一點嗎? 「只添加第一個組件」是什麼意思? –

+0

你添加更多,但它們重疊..使用佈局參數和規則,使新的一個出現在另一個下面,有quesiton和答案嘗試使用谷歌搜索 –

+0

驗證佈局的方向 – zicos22

回答

1

你的layout_cartelle_immagini聲明在哪裏?

也許它是一個水平的LinearLayout,只需將方向設置爲垂直,並且您的代碼應該可以工作(考慮添加自定義視圖的代碼位於onClickListener中)。

1

試試這個。它會幫助你。

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

你的意思是: 'root.addView(custom,params);',對吧? – PapaSmurf

+0

@Sajmon我試過你的代碼,但我有同樣的問題,我有刪除視圖內的組件,並重新插入所有每當我添加新的自定義組件? – PapaSmurf

+0

@PapaSmurf它不是我的代碼,我只是編輯答案。你需要提供更多信息。 – Sajmon

相關問題