直接上代码:
private RadioGroup mRadioGroup;
RadioButton radioButton = new RadioButton(getActivity());
radioButton.setBackgroundResource(R.drawable.home_page_radiobutton_selector);
//把那个讨厌的圈圈去掉,当然你也可以自定义你的样式来替代圈圈
radioButton.setButtonDrawable(android.R.color.transparent);
//用selector资源设置文字选中/未选中颜色
radioButton.setTextColor(getActivity().getResources()
.getColorStateList(R.color.home_page_radiobutton_color_selector));
radioButton.setLines(1);//单行
radioButton.setPadding(10, 10, 10, 10);
mRadioGroup.addView(radioButton, LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
在res/color/home_page_radiobutton_color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="#ffffff"/>
<item android:color="#0743a5"/>
</selector>