实例化主LinearLayout:
LinearLayout linearLayout = findViewById(R.id.linearLayout);
申请动态的LInearLayout的变量名:
LinearLayout linearLayoutx;
然后就可以愉快的肆无忌惮的添加了,需要添加就调用一次addLinearLayout函数。
public void addLinearLayout() {
//实例化一个LinearLayout
linearLayoutx = new LinearLayout(this);
//设置LinearLayout属性(宽和高)
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//设置边距
layoutParams.setMargins(0, 5, 0, 5);
//将以上的属性赋给LinearLayout
linearLayoutx.setLayoutParams(layoutParams);
this.linearLayout.addView(linearLayoutx);
}