我想在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>
的第一次,我按下按鈕,自定義組件被添加,但所有其他時間不是。 第一個自定義元素沒有被覆蓋,它是唯一可見的!
你能描述一下你面臨的問題好一點嗎? 「只添加第一個組件」是什麼意思? –
你添加更多,但它們重疊..使用佈局參數和規則,使新的一個出現在另一個下面,有quesiton和答案嘗試使用谷歌搜索 –
驗證佈局的方向 – zicos22