我想在LinearLayout中動態添加自定義組件。Android應用程序,動態添加自定義組件
這是一張我的活動,在這裏我想插入一個自定義組件的代碼:
<LinearLayout
android:id="@+id/layout_cartelle_immagini"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/border_bottom"
android:orientation="vertical" >
</LinearLayout>
這是我的自定義組件:
<?xml version="1.0" encoding="utf-8"?>
<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" >
<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" />
<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" />
<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>
這是一段代碼,我用來添加組件:
LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View root = findViewById(R.id.layout_cartelle_immagini);
View custom = vi.inflate(R.layout.custom_list_folder, null);
...
..
((ViewGroup) root).addView(custom, 0);
所有工作正常,但自定義組件不一樣應用程序的eme,爲什麼?我該如何解決這個問題?
謝謝。