在代码中使用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);