Android中,怎么在代码中实现“toRightof”属性?

所属的布局是Relative,就是不会用代码根据外界条件动态实现这个属性。怎么让A控件用代码实现在B控件右边的功能?

RelativeLayout layout = new RelativeLayout(this);
TextView tv1 = new TextView(this);
tv1.setText("A");

TextView tv2 = new TextView(this);
tv2.setText("B");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
lp.addRule(RelativeLayout.RIGHT_OF, tv1.getId());

layout.addView(tv1);

layout.addView(tv2, lp);
这样

直接在xml中写就可以了,没必要写在activity中。