Android5.0 CheckBox颜色修改

Android5.0开始,CheckBox带有material design动画效果,其默认的样式如下图所示:
这里写图片描述
可以看到,在上图中,CheckBox的边框为灰色,当被选中后,填充色为绿色。
那么如果我们想要改变边框和填充色,同时也保存material design动画效果,应该怎么做呢。
在style.xml文件中新增一条:

<style name="My_CheckBox" parent="@android:style/Widget.Material.CompoundButton.CheckBox">
        <item name="android:colorControlActivated">@color/colorAccent</item> <item name="android:colorControlNormal">@color/colorPrimary</item> </style>

然后,设置CheckBox:

<CheckBox
    android:id="@+id/save_pass"
    android:layout_width="wrap_content" android:layout_height="wrap_content" android:theme="@style/My_CheckBox"/>

需要注意的是:

  1. colorControlNormalcolorControlActivated分别对应框架控件在普通状态和激活状态下的颜色;
  2. 在为CheckBox设置style时,需要使用android:theme="@style/My_CheckBox",使用style="@style/My_CheckBox"没有效果。
    我使用的Android Studio版本为2.2.3,手机上Android版本为5.0.2。

猜你喜欢

转载自www.cnblogs.com/Free-Thinker/p/9067360.html
今日推荐