请看例子:
LinearLayout temp = new LinearLayout(IndexActivity.this);
LinearLayout.LayoutParams temp11 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100,1);
temp11.setMargins(5, 5, 5, 5);
temp.setLayoutParams(temp11);
temp.setOrientation(LinearLayout.VERTICAL);
temp.setGravity(Gravity.CENTER_VERTICAL|Gravity.CENTER_HORIZONTAL);
((LinearLayout)findViewById(R.id.indexBody)).addView(temp);