【问题标题】:Checkbox accent unchecked checkbox复选框重音未选中复选框
【发布时间】:2015-06-21 20:16:15
【问题描述】:

问题

有没有办法为默认的未选中复选框颜色着色?这是现在的状态:

我希望 UNCHECKED 复选框具有重音颜色(在我的情况下为蓝色),因为它在选中的变体中。

我的配置

compile 'com.android.support:appcompat-v7:22.0.0'

<item name="colorAccent">@color/accent</item>

【问题讨论】:

    标签: android material-design


    【解决方案1】:

    您可以以任何您喜欢的形状和颜色显示复选框。

    添加一个可绘制的custom_checkbox.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" 
          android:drawable="@xml/checked" /> 
    <item android:state_pressed="true"
          android:drawable="@xml/checked" /> 
    <item android:drawable="@xml/unchecked" /> 
    </selector>
    

    现在您可以指定 checkedunchecked 的外观:

    checked.xml:

    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- set whatever color you like -->
    <stroke android:width="2px" android:color="@color/accent" />
    <size android:height="20dp" android:width="20dp"/>
    </shape>
    

    unchecked.xml:

    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="2px" android:color="#ffc0c0c0" />
    <size android:height="20dp" android:width="20dp"/>
    </shape>
    

    现在您可以将复选框的android:button 属性设置为@drawable/custom_checkbox,您应该会看到自己的彩色复选框!

    代码基于this answer

    【讨论】:

    • 感谢您的帮助!但该解决方案仅部分起作用。它确实将框架着色为蓝色,但检查状态缺少“V”。它也只是普通的框架。我需要了解如何将选中状态链接到原始复选框。有什么想法吗?
    • @android:drawable/btn_check,我猜。
    • 默认选择器是 \@android:drawable/checkbox_on_background。问题是它是一张图片。无论如何,谢谢你的帮助。
    猜你喜欢
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-17
    • 2012-06-29
    • 2014-04-28
    • 2021-05-03
    • 2021-05-17
    相关资源
    最近更新 更多