Android替CheckBox设置Style

Android为CheckBox设置Style

MainActivity如下:

package cc.testcheckboxstyle;

import android.os.Bundle;
import android.app.Activity;
/**
 * Demo描述:
 * 为CheckBox设置Style
 */
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}

}


main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试CheckBox的Style" 
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dip"
    />
    
    <CheckBox 
        android:id="@+id/checkBox"
        android:layout_width="100dip"
        android:layout_height="100dip"
        android:layout_centerInParent="true"
        style="@style/CheckBoxStyle"
    />

</RelativeLayout>


styles.xml如下:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="AppBaseTheme" parent="android:Theme.Light">
    </style>

    <style name="AppTheme" parent="AppBaseTheme">
    </style>

    <style name="CheckBoxStyle" parent="@android:style/Widget.CompoundButton.CheckBox">
        <item name="android:button">@drawable/checkbox_background</item>
    </style>
    
</resources>


checkbox_background.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_checked="true" android:drawable="@drawable/on"/>
   <item android:state_checked="false" android:drawable="@drawable/off"/>
</selector>