2013-08-29 41 views
0

我想在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,爲什麼?我該如何解決這個問題?

謝謝。

回答

1

發生這種情況是因爲充氣不知道它屬於哪個活動或應用程序。您需要在構造函數中提供此信息,或者從您的活動中獲取LayoutInflator而不是全局上下文。嘗試從您的活動中調用getLayoutInflator()並使用它來擴大您的佈局。它會以與您的活動相同的主題來誇大佈局。

0
public View inflate (int resource, ViewGroup root, boolean attachToRoot) 

參數

資源 ID爲一個XML佈局資源加載(例如,R.layout.main_page);

可選視圖,使其將所生成的分層結構的父(如果attachToRoot爲真),否則簡單地提供一組的LayoutParams值的返回的層次結構的根對象(如果attachToRoot是假的。)

attachToRoot膨脹層次結構是否應附加到根參數?如果爲false,則root僅用於爲XML中的根視圖創建LayoutParams的正確子類。 返回 充氣層次結構的根視圖。如果提供了root並且attachToRoot爲true,則這是root;否則它就是膨脹的XML文件的根源。

您必須使用此方法在根視圖中充填自定義視圖xml。

相關問題