方案【1】]最简单方案:(适用于Theme.AppCompat主题下)
在你的AppTheme应用主题下,修改colorAccent为你的颜色值
<item name="colorAccent">@color/colorAccentitem>
方案【2】或者增加colorControlActivated为你的颜色值 (适用于Theme.AppCompat主题下)
<item name="colorControlActivated">@color/app_baseitem>">
方案【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属性
方案【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中定义:
参数defStyleAttr---
默认使用的属性的资源id,其值为com.android.internal.R.attr.switchStyle系统属性中有定义
在系统主题中有赋值
sdk\platforms\android-22\data\res\values\themes_material.xml文件中Theme.Material主题下寻找:
- @style/Widget.Material.CompoundButton.Switch
sdk\platforms\android-22\data\res\values\styles_material.xml文件中找到:
OK~到这里就找到了Switch的默认属性配置,其中track为滑动轨迹,thumb为触摸的圆片.
然后打开对应的drawable文件看下Google怎么写的,比照这写就行了.
写这玩意好麻烦.详细的不写了.RI