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