方案【1】]最简单方案:(适用于Theme.AppCompat主题下)
在你的AppTheme应用主题下,修改colorAccent为你的颜色值
<item name="colorAccent">@color/colorAccent</item>
方案【2】或者增加colorControlActivated为你的颜色值 (适用于Theme.AppCompat主题下)
<item name="colorControlActivated">@color/app_base</item>">
方案【3】在你的布局文件重写Switch控件的track和thumb属性
track,其selector写法和CheckBox写法一样。参考:\sdk\platforms\android-22\data\res\drawable\switch_track_material.xml写法thumb,其selector需要写一些动画,参考:\sdk\platforms\android-22\data\res\drawable\switch_thumb_material_anim.xml写法
<Switch android:thumb="@drawable/selector_bt_base" android:track="@drawable/selected_point_shape" android:id="@+id/notice_switch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="19.5dp" />方案【4】在style文件里增加一个style,style的名为Widget.Material.CompoundButton.Switch
重写track和thumb属性<style name="Widget.Material.CompoundButton.Switch"> <item name="track">@drawable/switch_track_material</item> <item name="thumb">@drawable/switch_thumb_material_anim</item> <item name="switchTextAppearance">@style/TextAppearance.Material.Widget.Switch</item> <item name="textOn">@string/capital_on</item> <item name="textOff">@string/capital_off</item> <item name="background">?attr/selectableItemBackgroundBorderless</item> <item name="showText">false</item> </style>方案【5】此写法不够安全 (适用于Theme.AppCompat主题下SDK-22等版本)
在drawable目录下添加switch_track_material.xml和switch_thumb_material_anim.xml
分别写track和thumb的selector
原理:
以v-22为例,
1、Switch构造函数:
public Switch(Context context, AttributeSet attrs) { this(context, attrs, com.android.internal.R.attr.switchStyle); }2、最终调用的构造函数:public Switch(Context context, AttributeSet attrs, int defStyleAttr) { this(context, attrs, defStyleAttr, 0); }3、构造函数中读取属性的方法:
final TypedArray a = context.obtainStyledAttributes( attrs, com.android.internal.R.styleable.Switch, defStyleAttr, defStyleRes);参数attrs---为读取的布局文件中Switch中的属性,
com.android.internal.R.styleable.Switch---
属性集,在\sdk\platforms\android-22\data\res\values\attrs.xml中定义:
<declare-styleable name="Switch">
<!-- Drawable to use as the "thumb" that switches back and forth. --> <attr name="thumb" /> <!-- Drawable to use as the "track" that the switch thumb slides within. --> <attr name="track" format="reference" /> <!-- Text to use when the switch is in the checked/"on" state. --> <attr name="textOn" /> <!-- Text to use when the switch is in the unchecked/"off" state. --> <attr name="textOff" /> <!-- Amount of padding on either side of text within the switch thumb. --> <attr name="thumbTextPadding" format="dimension" /> <!-- TextAppearance style for text displayed on the switch thumb. --> <attr name="switchTextAppearance" format="reference" /> <!-- Minimum width for the switch component --> <attr name="switchMinWidth" format="dimension" /> <!-- Minimum space between the switch and caption text --> <attr name="switchPadding" format="dimension" /> <!-- Whether to split the track and leave a gap for the thumb drawable. --> <attr name="splitTrack" /> <!-- Whether to draw on/off text. --> <attr name="showText" format="boolean" /> </declare-styleable>参数defStyleAttr---默认使用的属性的资源id,其值为com.android.internal.R.attr.switchStyle系统属性中有定义 <attr name="switchStyle" format="reference" />其为引用类型。在系统主题中有赋值sdk\platforms\android-22\data\res\values\themes_material.xml文件中Theme.Material主题下寻找:<item name="switchStyle">@style/Widget.Material.CompoundButton.Switch</item>sdk\platforms\android-22\data\res\values\styles_material.xml文件中找到:<style name="Widget.Material.CompoundButton.Switch"> <item name="track">@drawable/switch_track_material</item> <item name="thumb">@drawable/switch_thumb_material_anim</item> <item name="switchTextAppearance">@style/TextAppearance.Material.Widget.Switch</item> <item name="textOn">@string/capital_on</item> <item name="textOff">@string/capital_off</item> <item name="background">?attr/selectableItemBackgroundBorderless</item> <item name="showText">false</item> </style>OK~到这里就找到了Switch的默认属性配置,其中track为滑动轨迹,thumb为触摸的圆片.然后打开对应的drawable文件看下Google怎么写的,比照这写就行了.写这玩意好麻烦.详细的不写了.RI