相对布局中的保证金

时间:2012-10-22 10:14:12

标签: android margin relativelayout

如何使用代码(而不是XML)在相对布局中设置边距?

我使用此代码但没有发生任何事情:

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);

1 个答案:

答案 0 :(得分:0)

尝试这个答案set the absolute position of a view in Android

如上面链接中所述,您应该使用

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);