代码创建一个居中的按钮(动态创建控件)

在代码中使用RelativeLayout实现即可,下面是详细步骤:
RelativeLayout mLayout = new RelativeLayout(this);
mLayout.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));

Button mButton = new Button(this);
RelativeLayout.LayoutParams mParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
mParams.addRule(RelativeLayout.CENTER_IN_PARENT);
mButton.setLayoutParams(mParams);
mButton.setText("Center");

mLayout.addView(mButton);
setContentView(mLayout);

你可能感兴趣的:(代码创建一个居中的按钮(动态创建控件))