Android style妙用

checkbox 实现switch效果

原生的switch并不好看,自定义又很麻烦,如果大家不追求那个滑动的动画效果的话,可以用我这个方法。
首先定义一个selector

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

然后写一个style

   <style name="SwitchCheckboxTheme" parent="@android:style/Widget.CompoundButton.CheckBox">
        <item name="android:button">@drawable/switch_checkbox_selector</item>
    </style>

最后把checkbox应用这个style,大功告成~~

    <CheckBox
               style="@style/SwitchCheckboxTheme"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               />
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容