纯用代码进行安卓布局,为什么RelativeLayout.BELOW这个设置不起作用
private int receiverid=0;
private int msgid=1;
private int btnid=2;
public AppTalk(Context context,ISystemMethods iSystemMethods) {
super(context);
this.parentContext=context;
this.ParentiCommunication=iSystemMethods;
this.mainlayout=new RelativeLayout(parentContext);
this.mainlayout.setBackgroundColor(Color.CYAN);
init();
}
void init(){
//本类RelativeLayout的布局管理器
LayoutParams lpmain=new LayoutParams(400,400);
//创建提示框容器
LinearLayout receivelayout=new LinearLayout(parentContext);
receivelayout.setId(receiverid);
LayoutParams lpreceivelayout=new LayoutParams(-1,50);
lpreceivelayout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
//创建提示框
TextView showreceive=new TextView(parentContext);
//显示与xxx的聊天框等文字提示
showreceive.setText("与xxxx正在进行聊天");
showreceive.setTextColor(Color.RED);
LayoutParams lpreceive=new LayoutParams(-1,-1);
receivelayout.addView(showreceive,lpreceivelayout);
//showmsg和inputmsg的父级容器
LinearLayout msglayout=new LinearLayout(parentContext);
msglayout.setId(msgid);
msglayout.setBackgroundColor(Color.BLUE);
LayoutParams lpmsg=new LayoutParams(400,350);
lpmsg.addRule(RelativeLayout.BELOW,receiverid);//这句为什么不起作用?
//我的本意试想放在receivelayout这个提示的下面的,结果msglayout排在最上面了
mainlayout.addView(receivelayout);
mainlayout.addView(msglayout,lpmsg);
addView(mainlayout, lpmain);
//mainlayout.addView(msg,new LayoutParams(-1, -2));
}
分享到:
------解决方案--------------------
两个relativelayout上下如下面列子
LinearLayout ll = new LinearLayout(this);
ll.setBackgroundColor(Color.RED);
ll.setLayoutParams(new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT));
RelativeLayout rl = new RelativeLayout(this);
rl.setBackgroundColor(Color.BLUE);
rl.setLayoutParams(new RelativeLayout.LayoutParams(100,100));