我正在尝试为以编程方式添加到RadioGroup的RadioButtons设置边距,但是失败。:RadioButtons已正确添加,但它们的边距为0 ...
有人可以帮忙吗?
布局
<RadioGroup android:id="@+id/rg_nav" android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="wrap_content">
</RadioGroup>
Activity
float density = getResources().getDisplayMetrics().density;
rg_nav = (RadioGroup) findViewById(R.id.rg_nav);
LinearLayout.LayoutParams params_rb = new LinearLayout.LayoutParams(
(int)(8*density),
(int)(8*density));
int margin = (int)(6*density);
params_rb.setMargins(margin, margin, margin, margin);
for(String url : product.list_url_pic){
RadioButton radio_btn = new RadioButton(ProductHome.this);
radio_btn.setButtonDrawable(R.drawable.rb_nav);
radio_btn.setId(rb_id++);
rg_nav.addView(radio_btn, params_rb);
}
最佳答案
我不得不更换
LinearLayout.LayoutParams params_rb = new LinearLayout.LayoutParams(
(int)(8*density),
(int)(8*density));
经过
RadioGroup.LayoutParams params_rb = new RadioGroup.LayoutParams(
(int)(8*density),
(int)(8*density));