2013-08-29 39 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。

相关问题