2013-03-05 128 views
0
<LinearLayout 
    android:id="@+id/LinearLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    tools:context=".MainActivity" > 

如何在代碼中編寫上述XML?動態設置佈局

android.widget.LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
       LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1); 

對不對?

+0

你試過了嗎? – kcoppock 2013-03-05 22:57:31

+2

你爲什麼問? – Simon 2013-03-05 22:57:46

回答

2
//Create the layout params 
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); 

//Create the LinearLayout 
LinearLayout ll = new LinearLayout(context); 
ll.setOrientation(LinearLayout.VERTICAL); //set orientation 
ll.setLayoutParams(params); //asign your layout params