我想在我的应用程序中使用默认复选框,但是我只希望复选框颜色在选中时变为红色。我尝试了buttonTint,但是当未选中时,它会使方框变成红色,因此不起作用。
最佳答案
一个相对简单的方法是将主题仅应用到您的复选框。从本质上讲,您将向您的styles.xml资源文件添加一种样式,如下所示。这样,您甚至可以在未选中时为复选框提供自定义颜色。但是,如果您只想使用黑色默认复选框,则可以不使用android:textColorSecondary。
styles.xml
//main style above add this below.
<style name="RedCheckbox">
<item name="colorAccent">#FF0000</item> //color when checked
<item name="android:textColorSecondary>#00FFFF</item> //color when unchecked.
</style>
然后,您需要将此应用到您的复选框。
<CheckBox
//rest of your checkbox setup
android:theme="@style/RedCheckbox" //this is the important line.
/>
您无需以编程方式执行任何操作,它只会在不同的状态上发生变化。结果将是:
未选中
已检查