以下代码可解决问题:
在你的Activity/Fragment(XML)中:
<Switch
android:id="@+id/sMuteNotifications"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_margin="1dp"
android:checked="true"
android:gravity="center_vertical"
android:switchMinWidth="56dp"
android:thumb="@drawable/bg_thumb" // Create this drawable
android:track="@drawable/bg_switch_states"/> // and this one too
bg_thumb:(使用此图片或其他任何你想要的图片)
在你的drawables
文件夹中创建它
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="oval" xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="@android:color/white"/>
<size android:width="28dp" android:height="28dp"/>
</shape>
bg_switch_states:
<?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/bg_on_switch"/>
<item android:state_checked="false" android:drawable="@drawable/bg_off_switch"/>
</selector>
bg_on_switch :
<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="18dip" />
<solid android:color="@color/colorPrimaryLight"/> // <---What ever color you want to use here
</shape>
bg_off_switch:<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="18dip" />
<solid android:color="@color/colorPrimaryDark"/> // <---What ever color you want to use here
</shape>
就是这样了...
希望这能对某人有所帮助。