代码如下:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 创建一个新的RelativeLayout
RelativeLayout relativeLayout = new RelativeLayout(this);
// 定义RelativeLayout布局属性
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
// 创建一个新的TextView
TextView tv = new TextView(this);
tv.setText("Test");
// 定义TextView里的布局属性
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
//设置 TextView的属性
tv.setLayoutParams(lp);
relativeLayout.addView(tv);
setContentView(relativeLayout, rlp);
}