【发布时间】:2017-03-03 12:27:33
【问题描述】:
我正在使用颜色选择器在我的 android 应用程序中为 Checkbox 设置属性 buttonTint,以使其在选中/取消选中时改变颜色。
这是我的复选框样式的 XML:
<style name="standard_checkbox">
<item name="android:layout_marginLeft">@dimen/checkbox_small_margin</item>
<item name="android:layout_marginRight">@dimen/checkbox_small_margin</item>
<item name="buttonTint">@color/checkbox_standard_selector</item>
</style>
这是我的选择器 XML:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/checkbox_checked" />
<item android:state_checked="false" android:color="@color/checkbox_unchecked" />
</selector>
但它不能正常工作。 Checkbox 最初可见时具有正确的颜色(灰色)。单击/选中时,它会变成应有的绿色。但是当它再次被取消选中时,由于某种原因它仍然是绿色的。
编辑
问题也可能是我在清单中选择的主题引起的。我正在使用这个自定义主题:
<style name="Theme.Transparent.NoActionBar" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
【问题讨论】:
-
检查和取消检查时如何处理颜色变化?
-
用选择器,在复选框的不同状态上定义不同的颜色(android:state_checked=true/false)
-
我的意思是onCheck改变了方法?
-
哦,那......我没有在我的代码中使用 onchecklistener.. 当另一个按钮被按下时,我会根据复选框状态做一些事情。所以只调用“checkbox.isChecked()”,没有别的
-
你为什么不使用
android命名空间buttonTint?android:buttonTint风格?
标签: android android-view android-styles android-checkbox