动态设置布局

时间:2013-03-05 22:49:23

标签: android

<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);

是吗?

1 个答案:

答案 0 :(得分: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