如何改变CheckBox的颜色?

如何更改Android中默认的CheckBox颜色?
默认情况下, CheckBox颜色是绿色的,我想改变这种颜色。
如果这是不可能的,请告诉我如何制作自定义CheckBox

您可以使用android:button="@drawable/your_check_drawable"属性来更改CheckBox的drawable。

你可以直接在xml中着色。 使用buttonTint框(从API级23开始)

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

您也可以使用appCompatCheckbox v7对较旧的API执行此操作:

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

你可以设置checkbox的android主题,以获取你想要在styles.xml中添加的颜色:

 <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下工作

程序化版本:

 int states[][] = {{android.R.attr.state_checked}, {}}; int colors[] = {color_for_state_checked, color_for_state_normal} CompoundButtonCompat.setButtonTintList(checkbox, new ColorStateList(states, colors)); 
 <CheckBox android:id="@+id/chk_remember_signup" android:layout_width="wrap_content" android:layout_height="wrap_content" android:buttonTint="@android:color/white" android:text="@string/hint_chk_remember_me" /> 

将这一行添加到styles.xml文件中:

 <style> <item name="android:colorAccent">@android:color/holo_green_dark</item> </style> 

我build议在android中使用他风格的方法来configuration内置的Android视图,在您的项目中添加新的风格:

 <style name="yourStyle" parent="Base.Theme.AppCompat"> <item name="colorAccent">your_color</item> <!-- for uncheck state --> <item name="android:textColorSecondary">your color</item> <!-- for check state --> </style> 

并添加这个风格的checkbox的主题:android:theme =“@ style / youStyle”

希望这可以帮助。

我面临同样的问题,我使用下面的技术解决scheme。 将btn_check.xmlandroid-sdk/platforms/android-#(version)/data/res/drawable drawable复制到项目的可绘制文件夹中,并将“开”和“关”图像状态更改为自定义图像。
那么你的XML将只需要android:button="@drawable/btn_check"

 <CheckBox android:button="@drawable/btn_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" /> 

如果你想使用不同的默认Android图标,你可以使用:

 android:button="@android:drawable/..." 

你可以在drawable中创build自己的xml,并使用它作为android:background =“@ drawable / your_xml”

在那里你可以给所有的边界angular落

 <item> <shape> <gradient android:endColor="#fff" android:startColor="#fff"/> <corners android:radius="2dp"/> <stroke android:width="15dp" android:color="#0013669e"/> </shape> </item> 

НужноиспользоватьbuttonTintи颜色select器

 <android.support.v7.widget.AppCompatCheckBox android:id="@+id/check" android:layout_width="wrap_content" android:layout_height="wrap_content" app:buttonTint="@color/checkbox_filter_tint"/> 

RES /颜色/ checkbox_filter_tint.xml

 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:color="@color/light_gray_checkbox" android:state_checked="false"/> <item android:color="@color/common_red" android:state_checked="true"/> </selector> 

您可以通过将其embedded<LinearLayout>来更改<CheckBox>的背景颜色。 然后将<LinearLayout>的背景颜色更改为所需的颜色。

如果您要使用android图标,如上所述..

 android:button="@android:drawable/..." 

..这是一个不错的select,但为了这个工作 – 我发现你需要添加切换逻辑来显示/隐藏复选标记,如下所示:

  checkBoxShowPwd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { // checkbox status is changed from uncheck to checked. public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { int btnDrawable = android.R.drawable.checkbox_off_background; if (isChecked) { btnDrawable = android.R.drawable.checkbox_on_background; } checkBoxShowPwd.setButtonDrawable(btnDrawable); } }); 

res->drawable下创build一个xml Drawable resource file ,并将其命名,例如checkbox_custom_01.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="@drawable/checkbox_custom_01_checked_white_green_32" /> <item android:state_checked="false" android:drawable="@drawable/checkbox_custom_01_unchecked_gray_32" /> </selector> 

上传你的自定义checkbox图像文件(我推荐PNG)到res->drawable文件夹。

然后进入你的布局文件,并将其checkbox更改为

 <CheckBox android:id="@+id/checkBox1" android:button="@drawable/checkbox_custom_01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:focusable="false" android:focusableInTouchMode="false" android:text="CheckBox" android:textSize="32dip"/> 

你可以自定义任何东西,只要android:button指向你以前创build的正确的XML文件即可。

新手注意事项:虽然这不是强制性的,但是在整个布局树中为您的checkbox命名一个唯一的ID是个好习惯。

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

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

colorControlNormal用于checkbox的普通视图,colorControlActivated用于勾选checkbox。

嗨这是黑暗的主题和轻的主题的主题代码。

 <attr name="buttonsearch_picture" format="reference"/> <attr name="buttonrefresh_picture" format="reference"/> <style name="Theme.Light" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@color/white</item> <item name="android:windowActionBar">false</item> <item name="android:textColorPrimary">@color/black</item> <item name="android:textColorSecondary">@color/black</item> <item name="android:textColor">@color/material_gray_800</item> <item name="actionOverflowButtonStyle">@style/LightOverflowButtonStyle</item> <item name="buttonsearch_picture">@drawable/ic_search_black</item> <item name="buttonrefresh_picture">@drawable/ic_refresh_black</item> </style> <style name="Theme.Dark" parent="Theme.AppCompat.NoActionBar"> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@color/white</item> <item name="android:windowActionBar">false</item> <item name="android:textColorPrimary">@color/white</item> <item name="android:textColorSecondary">@color/material_gray_500</item> <item name="android:textColor">@color/material_gray_800</item> <item name="actionOverflowButtonStyle">@style/DarkOverflowButtonStyle</item> <item name="buttonsearch_picture">@drawable/ic_search_white</item> <item name="buttonrefresh_picture">@drawable/ic_refresh_white</item> <item name="android:colorBackground">#ffffff</item> <item name="android:alertDialogTheme">@style/LightDialogTheme</item> <item name="android:alertDialogStyle">@style/LightDialogTheme</item> <!-- <item name="android:textViewStyle">@style/AppTheme.Widget.TextView</item>--> <item name="android:popupMenuStyle">@style/PopupMenu</item> </style> 

如果你想改变checkbox颜色,那么“colorAccent”属性将用于选中状态,“android:textColorSecondary”将用于取消选中状态。

“actionOverflowButtonStyle”将用于更改操作栏中溢出图标的颜色。

“buttonsearch_picture”属性将用于更改Action Bar中Action Button的色调颜色。这是style.xml中的自定义属性

 <attr name="buttonsearch_picture" format="reference"/> 

同样是刷新button,我在我的应用程序中使用。

“android:popupMenuStyle”属性用于获取Light主题中的Light主题popup菜单样式。

 <style name="PopupMenu" parent="Theme.AppCompat.Light.NoActionBar"> </style> 

这是我在Rocks Player App中使用的工具条代码。

  <android.support.v7.widget.Toolbar android:id="@+id/toolbar" app:contentInsetStart="0dp" android:title="Rocks Player" android:layout_width="match_parent" android:elevation="4dp" android:layout_height="48dp" app:layout_scrollFlags="scroll|enterAlways" android:minHeight="48dp" app:titleTextAppearance="@style/Toolbar.TitleText" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" android:background="?attr/colorPrimary" > </android.support.v7.widget.Toolbar> 

主题: –

  <style name="AppTheme0" parent="Theme.Light"> <item name="colorPrimary">#ffffff</item> <item name="colorPrimaryDark">#cccccc</item> <item name="colorAccent">#0294ff</item> </style> <style name="AppTheme1" parent="Theme.Dark"> <item name="colorPrimary">#4161b2</item> <item name="colorPrimaryDark">#4161b2</item> <item name="colorAccent">#4161b2</item> </style> 

你应该尝试下面的代码。 它正在为我工​​作。

 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/checked" android:state_checked="true"> <color android:color="@color/yello" /> </item> <!-- checked --> <item android:drawable="@drawable/unchecked" android:state_checked="false"> <color android:color="@color/black"></color> </item> <!-- unchecked --> <item android:drawable="@drawable/checked" android:state_focused="true"> <color android:color="@color/yello"></color> </item> <!-- on focus --> <item android:drawable="@drawable/unchecked"> <color android:color="@color/black"></color> </item> <!-- default --> </selector> 

和CheckBox

 <CheckBox Button="@style/currentcy_check_box_style" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="20dp" android:text="@string/step_one_currency_aud" />