我如何改变默认的复选框的颜色在Android? 默认情况下,复选框的颜色是绿色,我想改变这个颜色。 如果不可能,请告诉我如何做一个自定义复选框?


当前回答

您可以直接在XML中更改颜色。使用buttonTint的盒子:(API级别23)

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:buttonTint="@color/CHECK_COLOR" />

你也可以使用旧API级别的appCompatCheckbox v7来做到这一点:

<android.support.v7.widget.AppCompatCheckBox 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:buttonTint="@color/COLOR_HERE" /> 

其他回答

您可以在“colors.xml”中使用以下两个属性

<color name="colorControlNormal">#eeeeee</color>
<color name="colorControlActivated">#eeeeee</color>

colorControlNormal用于复选框的正常视图,colorControlActivated用于复选框被选中时的视图。

如果你的minSdkVersion是21+使用android:buttonTint属性来更新复选框的颜色:

<CheckBox
  ...
  android:buttonTint="@color/tint_color" />

在使用AppCompat库和支持低于21的Android版本的项目中,你可以使用buttonTint属性的compat版本:

<CheckBox
  ...
  app:buttonTint="@color/tint_color" />

在这种情况下,如果你想子类化一个CheckBox,不要忘记使用AppCompatCheckBox代替。

之前的回答:

你可以使用android:button="@drawable/your_check_drawable"属性来改变checkboxes的可绘制性。

buttonTint工作为我尝试

android:buttonTint=“@color/white”

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:id="@+id/agreeCheckBox"
    android:text="@string/i_agree_to_terms_s"
    android:buttonTint="@color/white"
    android:layout_below="@+id/avoid_spam_text"/>

你可以设置复选框的android主题来获得你想要的样式的颜色。

<style name="checkBoxStyle" parent="Base.Theme.AppCompat">
    <item name="colorAccent">CHECKEDHIGHLIGHTCOLOR</item>
    <item name="android:textColorSecondary">UNCHECKEDCOLOR</item>
</style>

然后在布局文件中:

<CheckBox
     android:theme="@style/checkBoxStyle"
     android:id="@+id/chooseItemCheckBox"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"/>

不像使用android:buttonTint="@color/CHECK_COLOR"这个方法在Api 23下工作

您可以通过将<CheckBox>嵌入到<LinearLayout>中来更改它的背景颜色。然后将<LinearLayout>的背景色更改为您想要的颜色。