//创建线性linearlayout布局对象
LinearLayout ll = new LinearLayout(this);
//设置linearlayout布局方向
ll.setOrientation(LinearLayout.VERTICAL);
//得到布局或控件高度
int height = LinearLayout.LayoutParams.MATCH_PARENT;
//得到布局或控件宽度
int width = LinearLayout.LayoutParams.MATCH_PARENT;
//给linearlayout布局设置宽高
ll.setLayoutParams(new LinearLayout.LayoutParams(width, height));
//创建TextView控件对象
TextView textView = new TextView(this);
//设置文字
textView.setText("代码写布局");
//设置textview中文字的相对位子
textView.setGravity(Gravity.CENTER);
//把textview控件加入到linearlayout中
ll.addView(textView, new LinearLayout.LayoutParams(width, height));
setContentView(ll);
时间: 2024-10-17 22:01:52