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));
}
5 个解决方案
#1
两个relativelayout上下如下面列子
lpmsg.addRule(RelativeLayout.BELOW,receiverid) ;//receiverid 因为id为0不生效
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));
RelativeLayout rlTop = new RelativeLayout(this);
RelativeLayout.LayoutParams rlTopParams = new RelativeLayout.LayoutParams(40,40);
rlTopParams.addRule(RelativeLayout.CENTER_IN_PARENT);
rlTop.setBackgroundColor(Color.YELLOW);
rlTop.setLayoutParams(rlTopParams);
rl.addView(rlTop);
ll.addView(rl);
lpmsg.addRule(RelativeLayout.BELOW,receiverid) ;//receiverid 因为id为0不生效
#2
这个确实是原因之一,另外我做了一些实验,想问一下
是不是
RelativeLayout 下使用 lpmsg.addRule(RelativeLayout.BELOW,receiverid);这样的根据Id定位的可以,
而LinearLayout没有效果?
毕竟后者是线性的
#3
恩,LinearLayout没有,只有RelativeLayout才有below,left,right的定位。
#4
最后一问题,你看着什么情况?中间多了一条线。
代码:
// 添加两个选择按钮
// 关闭按钮
Button btnNO = new Button(parentContext);
btnNO.setText("关 闭");
btnNO.setTextSize(9);
// btnNO.setBackgroundColor(Color.YELLOW);
LayoutParams lpbtnNO = new LayoutParams(80, 50);
lpbtnNO.setMargins(200, 0, 10, 5);
lpbtnNO.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lpbtnNO.addRule(RelativeLayout.BELOW, inputmsgId);
mainlayout.addView(btnNO, lpbtnNO);
//确定按钮
Button btnOK=new Button(parentContext);
// btnOK.setBackgroundColor(Color.YELLOW);
btnOK.setText("发 送");
btnOK.setTextSize(9);
LayoutParams lpbtnOK=new LayoutParams(80,50);
lpbtnOK.setMargins(10, 0, 10, 5);
lpbtnOK.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lpbtnOK.addRule(RelativeLayout.BELOW, inputmsgId);
mainlayout.addView(btnOK, lpbtnOK);
addView(mainlayout, lpmain);
#5
没见过这种现象,可以去掉图片之类的试试分析,看看是哪个引起的。
#1
两个relativelayout上下如下面列子
lpmsg.addRule(RelativeLayout.BELOW,receiverid) ;//receiverid 因为id为0不生效
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));
RelativeLayout rlTop = new RelativeLayout(this);
RelativeLayout.LayoutParams rlTopParams = new RelativeLayout.LayoutParams(40,40);
rlTopParams.addRule(RelativeLayout.CENTER_IN_PARENT);
rlTop.setBackgroundColor(Color.YELLOW);
rlTop.setLayoutParams(rlTopParams);
rl.addView(rlTop);
ll.addView(rl);
lpmsg.addRule(RelativeLayout.BELOW,receiverid) ;//receiverid 因为id为0不生效
#2
这个确实是原因之一,另外我做了一些实验,想问一下
是不是
RelativeLayout 下使用 lpmsg.addRule(RelativeLayout.BELOW,receiverid);这样的根据Id定位的可以,
而LinearLayout没有效果?
毕竟后者是线性的
#3
恩,LinearLayout没有,只有RelativeLayout才有below,left,right的定位。
#4
最后一问题,你看着什么情况?中间多了一条线。
代码:
// 添加两个选择按钮
// 关闭按钮
Button btnNO = new Button(parentContext);
btnNO.setText("关 闭");
btnNO.setTextSize(9);
// btnNO.setBackgroundColor(Color.YELLOW);
LayoutParams lpbtnNO = new LayoutParams(80, 50);
lpbtnNO.setMargins(200, 0, 10, 5);
lpbtnNO.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lpbtnNO.addRule(RelativeLayout.BELOW, inputmsgId);
mainlayout.addView(btnNO, lpbtnNO);
//确定按钮
Button btnOK=new Button(parentContext);
// btnOK.setBackgroundColor(Color.YELLOW);
btnOK.setText("发 送");
btnOK.setTextSize(9);
LayoutParams lpbtnOK=new LayoutParams(80,50);
lpbtnOK.setMargins(10, 0, 10, 5);
lpbtnOK.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lpbtnOK.addRule(RelativeLayout.BELOW, inputmsgId);
mainlayout.addView(btnOK, lpbtnOK);
addView(mainlayout, lpmain);
#5
没见过这种现象,可以去掉图片之类的试试分析,看看是哪个引起的。