如何使用AppCompat覆盖复选框颜色 [英] How to override CheckBox color using AppCompat

查看:1176
本文介绍了如何使用AppCompat覆盖复选框颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的复选框被选中时,我想要的颜色是@ color / colorPrimaryLight。我可以通过设置

为Lollipop设置它。

  android:buttonTint =@ color / colorPrimaryLight

,但我不知道如何在Lollipop之前做任何事情,而不改变

 < item name =colorControlActivated> @ color / white< / item& 




Lollipop:

 < CheckBox 
android:buttonTint =@ color / colorPrimaryLight
Android:id =@ + id / SwapCheckbox
android:layout_marginTop =@ dimen / margin_top
android:layout_marginLeft =@ dimen / margin_left_right
android:layout_marginRight =@ dimen / margin_left_right
android:layout_width =wrap_content
android:layout_height =wrap_content/>

以及Lollipop之前的任何内容(高于4.0.3):

 < CheckBox 
android:id =@ + id / SwapCheckbox
android:layout_marginTop =@ dimen / margin_top
android:layout_marginLeft =@ dimen / margin_left_right
android:layout_marginRight =@ dimen / margin_left_right
android:layout_width =wrap_content
android:layout_height =wrap_content/ >

我也有我的应用程序的主题:

 < style name =AppThemeparent =Theme.AppCompat.Light> 
// ...
< item name =colorControlActivated> @ color / white< / item>
// ...
< / style>

我不能改变colorControlActivated,因为它在我的ViewPager的指标必须是白色的。有没有反正改变我的复选框的背景颜色(即使它是默认的Holo风格,但最好是设置为@ color / colorPrimaryLight)?

解决方案

在以下应用程序兼容主题中使用以下属性

 < style name = AppThemeparent =Theme.AppCompat.Light> 
< item name =colorAccent> @ color / colorAccent< / item>
< / style>

并使用 android.support.v7.widget.AppCompatCheckBox,


When my checkbox is checked, I want the color to be @color/colorPrimaryLight. I can set it that way for Lollipop by setting

android:buttonTint="@color/colorPrimaryLight"

in the CheckBox but I don't know how to do it for anything before Lollipop without changing

    <item name="colorControlActivated">@color/white</item>

in my theme.

CheckBox for Lollipop:

<CheckBox
android:buttonTint="@color/colorPrimaryLight"
android:id="@+id/SwapCheckbox"
android:layout_marginTop="@dimen/margin_top"
android:layout_marginLeft="@dimen/margin_left_right"
android:layout_marginRight="@dimen/margin_left_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

and for anything before Lollipop (and higher than 4.0.3):

<CheckBox
android:id="@+id/SwapCheckbox"
android:layout_marginTop="@dimen/margin_top"
android:layout_marginLeft="@dimen/margin_left_right"
android:layout_marginRight="@dimen/margin_left_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

And I also have in the theme for my app:

<style name="AppTheme" parent="Theme.AppCompat.Light">
//...
<item name="colorControlActivated">@color/white</item>
//...
</style>

I can't change colorControlActivated as it must be white for the indicator in my ViewPager. Is there anyway to change the background color of my checkbox (even if it is to the default Holo style however best so that it is set to @color/colorPrimaryLight)?

解决方案

Use the following property in the App compat theme like below

<style name="AppTheme" parent="Theme.AppCompat.Light">
  <item name="colorAccent">@color/colorAccent</item>
</style>

and use the android.support.v7.widget.AppCompatCheckBox instead of CheckBox

这篇关于如何使用AppCompat覆盖复选框颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆