如何使用代码(不是 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);
最佳答案
试试这个答案 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);
关于android - 相对布局中的边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13009233/