android CheckBox修改颜色

博客介绍了在Android中修改按钮颜色的方法。若仅修改颜色无需改图像,使用buttonTint属性即可,但默认覆盖属性会使未选择时按钮颜色也被覆盖。若要实现check不同状态颜色不同,可在color文件夹下创建check_color.xml文件,颜色未变时可build一下。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果只想修改颜色,不需要修改图像的话,使用buttonTint属性足够

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:buttonTint="#990000"
                android:checked="true"/>

buttonTint会交会button的颜色,而默认的交会属性是覆盖,但这样有个问题,android:checked="false"时,button颜色也会被覆盖,达不到我们希望的未选择时置灰的效果,所以如果需要check不同时,颜色不同可以这样写:

color文件夹下创建文件:check_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#888888" android:state_checked="false"/>
    <item android:color="#990000" android:state_checked="true"/>
</selector>
            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:buttonTint="@color/check_color"/>

如果颜色没变,build一下吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值