修改Switch开关按钮的颜色

方案【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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值