Android CheckBox修改选中颜色并去除选中时的水波纹效果

前言

都知道Android原生的控件颜色比较辣眼睛,所以实际开发中都会有改动,而选中框是在实际开发中常用的,比如同意这个协议就勾选上。

先写一个控件

<CheckBox
        android:text="同意服务协议"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

运行效果

在这里插入图片描述

这就是原生的控件,请问这个颜色好看吗?
所以要改,在res文件夹下的values中的styles.xml文件中增加如下代码:

	<!--复选框样式,未勾选时为灰色,勾选好为黄色-->
    <style name="MyCheckBox" parent="Theme.AppCompat.Light">
        <item name="colorControlNormal">#ADB6AF</item>
        <item name="colorControlActivated">#F7F13D</item>
    </style>

然后在布局文件中应用这个样式:

	<CheckBox
        android:text="同意服务协议"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:theme="@style/MyCheckBox"/>

运行效果:
在这里插入图片描述

这种修改方式是不同于通过background的来切换的,我保留了这个控件选中和取消选中的动画效果,只修改了选中前后的颜色,这种方式是比较好的,android:theme="@style/MyCheckBox",MyCheckBox是刚才我定义的样式名称。
去除选中时的水波纹效果其实一行代码就搞定了,就是把背景值为透明即可,@android:color/transparent

修改布局文件:

	<CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:text="同意服务协议"
        android:theme="@style/MyCheckBox" />

这时你再运行起来就可以了。

发布了51 篇原创文章 · 获赞 17 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_38436214/article/details/105192967
今日推荐