Android5.0开始,CheckBox带有material design动画效果,其默认的样式如下图所示:
可以看到,在上图中,CheckBox的边框为灰色,当被选中后,填充色为绿色。
那么如果我们想要改变边框和填充色,同时也保存material design动画效果,应该怎么做呢。
在style.xml文件中新增一条:
<style name="MyCheckBox" parent="@android:style/Widget.Material.CompoundButton.CheckBox">
<item name="android:colorControlActivated">@color/colorWhitePure</item>
<item name="android:colorControlNormal">@color/colorWhitePure</item>
</style>
然后,设置CheckBox:
<CheckBox
android:id="@+id/checkbox_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/layout_password"
android:layout_marginStart="@dimen/dp_235"
android:layout_marginTop="@dimen/dp_22"
android:scaleX="0.7"
android:scaleY="0.7"
android:text="自动登录"
android:textColor="#FFFFFF"
android:textSize="@dimen/sp_20"
android:theme="@style/MyCheckBox" />
需要注意的是:
- colorControlNormal和colorControlActivated分别对应框架控件在普通状态和激活状态下的颜色;
- 在为CheckBox设置style时,需要使用
android:theme="@style/MyCheckBox",
使用style="@style/MyCheckBox"
没有效果。 - 我使用的Android Studio版本为3.0.1,需要指定最小sdk版本是21,不然style文件会报错。
如对此有疑问,请联系qq1164688204。
推荐Android开源项目
项目功能介绍:RxJava2和Retrofit2项目,添加自动管理token功能,添加RxJava2生命周期管理,使用App架构设计是MVP模式和MVVM模式,同时使用组件化,部分代码使用Kotlin,此项目持续维护中。
项目地址:https://gitee.com/urasaki/RxJava2AndRetrofit2