2010-11-23 44 views
1

我有一個xml main.xml中,其中有幾個佈局Android的佈局形式的活動

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainl" 
    android:tag="aa" 
    android:orientation="vertical" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent"> 

</LinearLayout> 

是否有可能在LinearLayout從裏面在運行時的活動增加一個佈局。我能夠在活動中創建佈局(不使用xml),但是在運行時將此佈局添加到xml時出現問題,任何人都有解決方案

回答

2

您不會在運行時將它添加到XML中,但是您可以將它添加到佈局,我認爲這是你想要做的。

parent.addView(child); 
+0

當我試圖這樣我得到的NullPointerException之前, – evan 2010-11-23 15:37:55

2

使用下面的代碼

LinearLayout parent,child; 

child=new LinearLayout(this); 

parent=(LinearLayout)findviewById(R.id.mainl); 

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); 

parent.addView(child,lp);