简体   繁体   English

相对布局中的保证金

[英]Margin in Relative Layout

How to set margin in relative layout with code (not XML)? 如何用代码(而不是XML)设置相对布局的边距?

I use this code but nothing happened: 我使用此代码但没有发生任何事情:

RelativeLayout rlDetail = new RelativeLayout(context);
rlDetail.setBackgroundResource(R.drawable.bg_round);
RelativeLayout.LayoutParams rlDetailParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlDetailParams.setMargins(35, 10, 35, 0);
rlDetail.setLayoutParams(rlDetailParams);

try this answer set the absolute position of a view in Android 试试这个答案设置Android中视图的绝对位置

as mentioned in the link above you should use 如上面链接中所述,您应该使用

TextView tv1 = new TextView(context);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = needed_margin;
params.topMargin = needed_margin;

// add your textview like this
rl.addView(tv1, params);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM