public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);// 定义布局管理器的参数
layout.setOrientation(LinearLayout.VERTICAL);// 所有组件垂直摆放
// 定义显示组件的布局管理器,为了简单,本次只定义一个TextView组件
LinearLayout.LayoutParams text = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);// 定义文本显示组件
TextView txt = new TextView(this);
txt.setLayoutParams(text);// 配置文本显示组件的参数
txt.setText("动态生成内容");// 配置显示文字
txt.setTextSize(20);
layout.addView(txt, text);
super.setContentView(layout, param);
}
}