setmargin在android中的相对布局中不起作用

setmargin在android中的相对布局中不起作用,第1张

概述我以编程方式在相对布局中创建了2个按钮.我希望按钮之间没有余量空间,并试图通过使用’setmargin’来实现这一点,但失败了.下面是代码. //creating the relative layout RelativeLayout relativeLayout = new RelativeLayout(this); RelativeLayout.LayoutParams rlp = ne 我以编程方式在相对布局中创建了2个按钮.我希望按钮之间没有余量空间,并试图通过使用’setmargin’来实现这一点,但失败了.下面是代码.

//creating the relative layout  relativeLayout relativeLayout = new relativeLayout(this);  relativeLayout.LayoutParams rlp = new relativeLayout.LayoutParams(            relativeLayout.LayoutParams.MATCH_PARENT,relativeLayout.LayoutParams.MATCH_PARENT);   //creating buttons    button button1 = new button(this);    button1.setID(R.ID.button1);    relativeLayout.LayoutParams params1 = new relativeLayout.LayoutParams(            SCR_W/2,relativeLayout.LayoutParams.WRAP_CONTENT);//SCR_W is the device screenwIDth    params1.addRule(relativeLayout.AliGN_PARENT_left);    params1.addRule(relativeLayout.AliGN_PARENT_top);    params1.setmargins(0,0);    button1.setLayoutParams(params1);    relativeLayout.addVIEw(button1);      button button2 = new button(this);    button2.setID(R.ID.button2);    relativeLayout.LayoutParams params2 = new relativeLayout.LayoutParams(            SCR_W/2,relativeLayout.LayoutParams.WRAP_CONTENT);    params2.addRule(relativeLayout.AliGN_PARENT_left);    params2.addRule(relativeLayout.BELOW,button1.getID());    params2.setmargins(0,0);    button2.setLayoutParams(params2);    relativeLayout.addVIEw(button2);//setting the relative layout as the contentvIEw     setContentVIEw(relativeLayout,rlp);@H_403_12@  

提前致谢..

解决方法 我尝试了你的代码,对我有用,

两个按钮位于距离0.要检查它,只需为每个按钮设置不同的颜色,您将看到它们在一起.

button1.setBackgroundcolor(color.BLUE);button2.setBackgroundcolor(color.RED);@H_403_12@                            	          总结       

以上是内存溢出为你收集整理的setmargin在android中的相对布局中不起作用全部内容,希望文章能够帮你解决setmargin在android中的相对布局中不起作用所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1126855.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存