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





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值