android 自定义checkBox的样式
生活随笔
收集整理的這篇文章主要介紹了
android 自定义checkBox的样式
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
今天,隨便講講自定義CheckBox的樣式。
第一種方法:
1.在drawable文件新建checkbox_style.xml。
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/checkbox_pressed" android:state_checked="true"/><item android:drawable="@drawable/checkbox_normal" android:state_checked="false"/><item android:drawable="@drawable/checkbox_normal"/></selector>
2.定義一個style,使用上面的xml樣式
<style name="CustomCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox"><item name="android:button">@drawable/checkbox_style</item> </style>
3.把CheckBox的樣式設置為自定義的樣式
<CheckBoxandroid:id="@+id/select_all"android:layout_width="wrap_content"android:layout_height="wrap_content"style="@style/CustomCheckboxTheme" />
使用到的圖片資源
checkbox_normal.png
checkbox_pressed.png
第二種方法:
第一步:定義drawable樣式文件
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/checked" android:state_checked="true" /><item android:drawable="@drawable/checked_false" android:state_checked="false" /><item android:drawable="@drawable/checked" android:state_pressed="true" /><item android:drawable="@drawable/checked_false" /> </selector>
第二步:在xml文件中配置
<CheckBoxandroid:id="@+id/checkBox"android:layout_width="25dp"android:layout_height="25dp"android:layout_gravity="center"android:background="@drawable/checkbox_style"android:button="@null"android:layout_marginLeft="10dp" />
主要定義drawable時,需要把button屬性設置為null。
android 自定義checkBox的樣式就講完了。
就這么簡單。
總結(jié)
以上是生活随笔為你收集整理的android 自定义checkBox的样式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android AppCompatEdi
- 下一篇: android 布局适配虚拟键适配