Android 单选组合框

Android RadioGroup和RadioButton案例及详解 https://my.oschina.net/amigos/blog/59261

RadioButton默认的样式(Android-25)

在Android/sdk/platforms/android-25/data/res/values/styles.xml:374(以前的版本好像带有item name="background"属性)

<style name="Widget.CompoundButton.RadioButton">
    <item name="button">?attr/listChoiceIndicatorSingle</item>
</style>
在Android/sdk/platforms/android-25/data/res/values/themes.xml:154中

<item name="listChoiceIndicatorSingle">@drawable/btn_radio</item>
在Android/sdk/platforms/android-25/data/res/drawable/btn_radio.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:state_window_focused="false"
          android:drawable="@drawable/btn_radio_on" />
    <item android:state_checked="false" android:state_window_focused="false"
          android:drawable="@drawable/btn_radio_off" />
          
    <item android:state_checked="true" android:state_pressed="true"
          android:drawable="@drawable/btn_radio_on_pressed" />
    <item android:state_checked="false" android:state_pressed="true"
          android:drawable="@drawable/btn_radio_off_pressed" />

    <item android:state_checked="true" android:state_focused="true"
          android:drawable="@drawable/btn_radio_on_selected" />
    <item android:state_checked="false" android:state_focused="true"
          android:drawable="@drawable/btn_radio_off_selected" />

    <item android:state_checked="false" android:drawable="@drawable/btn_radio_off" />
    <item android:state_checked="true" android:drawable="@drawable/btn_radio_on" />
</selector>

如果需要自定义RadioButton的样式,可以采用

radioButton.setButtonDrawable(null); //去除RadioButton前面的圆点
radioButton.setBackgroundResource(R.drawable.XXX);

RadioGroup动态添加RadioButton(实现水平指示点)

//RadioGroup实现水平指示点
private RadioGroup mRadioGroup = (RadioGroup) findViewById(R.id.XXX);
mRadioGroup.removeAllViews();
for (int i = 0; i < COUNT; i++) { //COUNT个指示点
	RadioButton radioButton = new RadioButton(mContext);
	radioButton.setId(i);
	RadioGroup.LayoutParams lp = new RadioGroup.LayoutParams(width, height);//radioButton的宽高
	lp.gravity = Gravity.CENTER;
	radioButton.setButtonDrawable(null);
	radioButton.setBackgroundResource(R.drawable.XXX);//radioButton的自定义样式
	if (i == 0) {
	   radioButton.setChecked(true);
	} else {
	   lp.setMargins(left, 0, 0, 0);//两个radioButton之间的距离
	}
	radioButton.setLayoutParams(lp);
	mRadioGroup.addView(radioButton);
}





猜你喜欢

转载自blog.csdn.net/zhanhong39/article/details/75993795