1、设置margin
写一个LayoutParams,之后radiobutton使用setLayoutParams
2、设置setchecked后,第一项一只是选中状态
原来用的是 button.setchecked(trude);
修改为 group.check(button.getId),(注:group为radiobutton所在RadioGroup);
3、使用资源文件设置文字颜色时,颜色不变
button.settextcolor(getRecourse.getColor(R.xx.xxx))
修改为 button.settextcolor(getRecourse.getColorStatusList(R.xx.xxx))
附:代码
RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.leftMargin = 12; RadioButton button = new RadioButton(GoodsActivity.this); button.setText("杂货"); button.setButtonDrawable(0); button.setPadding(12, 6, 12, 6); ColorStateList colors = getResources().getColorStateList(R.color.radio_color); button.setTextColor(colors); button.setBackgroundResource(R.drawable.redio_background); button.setLayoutParams(params); group_class.addView(button); if (i == 0) { group_class.check(button.getId()); }