Button btn1;
Btn1.setid (1001 );
Int width; // layout width;
Int btnwidth; // BTN width;
Int btnheight; // BTN height;
.......
Relativelayout. layoutparams P = new relativelayout. layoutparams (btnwidth, btnheight );
P. addrule (relativelayout. align_parent_top );
P. addrule (relativelayout. center_horizontal, relativelayout. True );
P. topmargin = config. topmargin;
// It must be set here; otherwise, the control that depends on the left and right alignment cannot be aligned.
// If this parameter is not set and only the horizontal center attribute is used, the leftmargin value of the 1001 control is 0, leading to left and right alignment failure.
P. leftmargin = (width-btnwidth)/2;
//
Button btn2;
Relativelayout. layoutparams P = new relativelayout. layoutparams (btnwidth, btnheight );
P. addrule (relativelayout. align_parent_top );
P. addrule (relativelayout. align_left, 1001); // to take effect, the control 1001 must be added with leftmargin.
P. topmargin = config. topmargin;
//
Button btn3;
Relativelayout. layoutparams P = new relativelayout. layoutparams (btnwidth, btnheight );
P. addrule (relativelayout. align_parent_top );
// To take effect, the control 1001 must be added with leftmargin/right margin.
P. addrule (relativelayout. align_right, 1001 );
P. topmargin = config. topmargin;
Relativelayout. layoutparams use addrule to solve the problem