自定义checkbox

首先先定义一个CheckBox。 关键是style="@style/MyCheckBox" 
<CheckBox
Android:id="@+id/login_cb_quite"
Android:layout_width="wrap_content"

Android:layout_height="wrap_content"
Android:layout_gravity="right"
ndroid:textColor="#7fffffff"
Android:text="@string/opt_silent"
style="@style/MyCheckBox" />

style.xml:

<resources>
    <style name="MyCheckBox" parent="@Android :style/Widget.CompoundButton.CheckBox">
        <item name="Android:textSize">16.0sp</item>
        <item name="Android:textColor">@color/qq_checkbox_text_color</item>
        <item name="Android:paddingLeft">28.0px</item>
        <item name="Android:button">@drawable/qq_btn_check</item>
    </style>
</resources>
这里的关键是<item name="Android:button">@drawable/qq_btn_check</item>
这里将button的样式设置成qq_btn_check.xml中的设置

qq_btn_check.xml:

<selector xmlns:Android="http://schemas.android.com/apk/res/android">
<item Android:state_window_focused="false" android:state_enabled="true"
Android:state_checked="true" android:drawable="@drawable/btn_check_on" />
<item Android:state_window_focused="false" android:state_enabled="true"
Android:state_checked="false" android:drawable="@drawable/btn_check_off" />
<item Android:state_enabled="true" android:state_checked="true"
Android:state_pressed="true" android:drawable="@drawable/btn_check_on_pressed" />
<item Android:state_enabled="true" android:state_checked="false"
Android:state_pressed="true" android:drawable="@drawable/btn_check_off_pressed" />
<item Android:state_focused="true" android:state_enabled="true"
Android:state_checked="true" android:drawable="@drawable/btn_check_on_selected" />
<item Android:state_focused="true" android:state_enabled="true"
Android:state_checked="false" android:drawable="@drawable/btn_check_off_selected" />
<item Android:state_enabled="true" android:state_checked="false"
Android:drawable="@drawable/btn_check_off" />
<item Android:state_enabled="true" android:state_checked="true"
Android:drawable="@drawable/btn_check_on" />
</selector>


设置当选中和未选中的的样式图都可以定义自己想要设置的样式

一个自定义的checkBox就出来了 

 

你可能感兴趣的:(自定义checkbox)