3

如何使用代码(不是 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);
4

1 回答 1

0

试试这个答案在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);
于 2012-10-22T10:30:34.377 回答