RadioButton和CheckBox自定义样式

效果图如下:

RadioButton和CheckBox自定义样式_第1张图片


1,通过继承RadioButton,重写onDraw(Canvas canvas)方法,实现自定义效果的的RadioButton

关键代码如下:

<span style="font-size:18px;"><span style="font-size:18px;">	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int cx = getWidth() / 2;
		int cy = getHeight() / 2;
		
		int wheelRadius = (int) ((1 - spaceRatio) * radius);<span style="white-space:pre">	</span>
		canvas.drawCircle(cx, cy, wheelRadius, wheelPaint); // 画内面的实心圆
		if (checked) {<span style="white-space:pre">	</span>// 当为选中状态时,画外环
			canvas.drawCircle(cx, cy, radius-strokeWidth/2, strokePaint);
		}
	}</span></span>



2,自定义CheckBox样式,实现在默认情况下为白色,选中状态为绿色

关键代码如下:

<span style="font-size:18px;"><span style="font-size:18px;"><CheckBox
            android:id="@+id/Sunday"
            style="@style/radiobutton_week_text"
            android:text="@string/sunday" /></span></span>
<span style="font-size:18px;"><span style="font-size:18px;"> <style name="radiobutton_week_text">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_weight">1.0</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:button">@null</item>
        <item name="android:gravity">center</item>
        <item name="android:lines">1</item>
        <item name="android:textSize">16sp</item>
        <item name="android:textColor">@color/selector_rbt_week_text</item>
        <item name="android:background">@null</item>
    </style></span></span>
在res/color文件夹下的selector_rbt_week_text.xml

<span style="font-size:18px;"><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/common_theme" android:state_checked="true" />
    <item android:color="@color/common_font_white" />
</selector></span></span>



项目下载地址:http://download.csdn.net/detail/u010134293/9196409


你可能感兴趣的:(checkbox,样式,RadioButton)