以编程方式在RadioButton上设置的保证金未应用

我试图设置一个边缘,以RadioButtons编程方式添加到RadioGroup,但它失败了:RadioButtons被正确添加,但它们有0个边距……

有人可以帮忙吗?

在此处输入图像描述

布局

  

活动

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

    尝试这个:

     TableRow.LayoutParams rg_params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 3f); RadioGroup radio_group=new RadioGroup(this); radio_group.setOrientation(RadioGroup.HORIZONTAL); radio_group.setLayoutParams(rg_params); RadioButton dry=new RadioButton(this); RadioGroup.LayoutParams params_soiled = new RadioGroup.LayoutParams(getBaseContext(), null); params_soiled.setMargins(10, 0, 10, 0); dry.setLayoutParams(button_params); 

    你的代码似乎是正确的。 尝试将LayoutParams直接应用于按钮

     radio_btn.setButtonDrawable(R.drawable.rb_nav); radio_btn.setId(rb_id++); rg_nav.addView(radio_btn); radio_btn.setLayoutParams(params_rb);