Android style妙用

2017-06-06  本文已影响0人  doubimonkey

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"
               />
上一篇 下一篇

猜你喜欢

热点阅读