Android5.0开始,CheckBox带有material design动画效果,其默认的样式如下图所示:
Android5.0 CheckBox颜色修改
可以看到,在上图中,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。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-16
  • 2021-10-12
  • 2021-08-16
  • 2021-04-15
  • 2022-01-18
猜你喜欢
  • 2022-12-23
  • 2021-05-01
  • 2021-09-26
  • 2021-04-18
  • 2021-08-08
  • 2021-10-28
  • 2021-10-17
相关资源
相似解决方案