Android: Button Positioning problem with RelativeLayout
I want to have this: B1 shall be under B0. B2 shall be right to B1. Like this:
B0
B1 B2But my code produces this:
B0 B2
B1Whats wrong with this code?:
RelativeLayout xml_layout = (RelativeLayout) findViewById(R.id.custom_layout_id);
Button[] b_test = new Button[4];
b_test[1] = new Button(getContext());
b_test[1].setId(1);
b_test[1].setText("B0");
RelativeLayout.LayoutParams lp0 = new RelativeLayout.LayoutParams(70,70);
lp0.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
xml_layout.addView(b_test[1], lp0);
b_test[2] = new Button(getContext());
b_test[2].setId(2);
b_test[2].setText("B1");
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(70,70);
lp1.addRule(RelativeLayout.BELOW, b_test[1].getId());
xml_layout.addView(b_test[2], lp1);
b_test[3] = new Button(getContext());
b_test[3].setId(3);
b_test[3].setText("B2");
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(70,70);
lp2开发者_开发百科.addRule(RelativeLayout.RIGHT_OF, b_test[2].getId());
xml_layout.addView(b_test[3], lp2);
Add the following line:
lp2.addRule(RelativeLayout.BELOW, b_test[1].getId());
and you should be all set.
精彩评论