Android Tinting Checkbox on pre v21
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26532045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tinting Checkbox on pre v21
提问by bluebyte
So, I want to apply tint to AppCompat Checkbox.
So, I want to apply tint to AppCompat Checkbox.
Everything works fine on Lollipop:
Everything works fine on Lollipop:
android:buttonTint="@color/purple_FF4081"
or this way:
or this way:
android:theme="@style/Theme.MyTheme.PurpleAccent"
But setting any of this params do not change anything on pre-Lollipop. Works only if I set colorAccent
for the app theme. But I don't want all widgets to change their look, just one checkbox.
Is there any way to do this without setting colored drawables?
But setting any of this params do not change anything on pre-Lollipop. Works only if I set colorAccent
for the app theme. But I don't want all widgets to change their look, just one checkbox.
Is there any way to do this without setting colored drawables?
回答by Daniel Wilson
Quick fyi that this has all changed now after the introduction of the AppCompatActivity and the new support libraries, for reference (outlined beautifully here)a checkbox can be tinted by using the theme
atttribute and setting the colorControlNormal
and colorControlActivated
:
Quick fyi that this has all changed now after the introduction of the AppCompatActivity and the new support libraries, for reference (outlined beautifully here)a checkbox can be tinted by using the theme
atttribute and setting the colorControlNormal
and colorControlActivated
:
styles.xml
styles.xml
<style name="MyCheckBox" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">@color/indigo</item>
<item name="colorControlActivated">@color/pink</item>
</style>
layout xml:
layout xml:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Check Box"
android:theme="@style/MyCheckBox"/>
回答by Anudeep Samaiya
You can color directly in the xml. Use buttonTint for the box: (as of API level 23)
You can color directly in the xml. Use buttonTint for the box: (as of API level 23)
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:buttonTint="@color/CHECK_COLOR" />
You can also do this using appCompatCheckbox v7 for older APIs:
You can also do this using appCompatCheckbox v7 for older APIs:
<android.support.v7.widget.AppCompatCheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:buttonTint="@color/COLOR_HERE" />
回答by androidseb
I needed to do it programmatically, after digging for a little while I finally found this solution (tested on Kitkat & Marshmallow), I'll just post it in case it helps someone:
I needed to do it programmatically, after digging for a little while I finally found this solution (tested on Kitkat & Marshmallow), I'll just post it in case it helps someone:
public static void setAppCompatCheckBoxColors(final AppCompatCheckBox _checkbox, final int _uncheckedColor, final int _checkedColor) {
int[][] states = new int[][]{new int[]{-android.R.attr.state_checked}, new int[]{android.R.attr.state_checked}};
int[] colors = new int[]{_uncheckedColor, _checkedColor};
_checkbox.setSupportButtonTintList(new ColorStateList(states, colors));
}
回答by Mina Fawzy
I have tried all answer but only this one works for me , add atttribute colorControlNormal
and colorControlActivated
to base style of whole activity (or application ) remove theme from your controller
I have tried all answer but only this one works for me , add atttribute colorControlNormal
and colorControlActivated
to base style of whole activity (or application ) remove theme from your controller
here is example
here is example
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<!-- colorPrimary is used for the default action bar background -->
<item name="colorPrimary">@color/colorPrimary</item>
<!-- colorPrimaryDark is used for the status bar -->
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<!-- colorAccent is used as the default value for colorControlActivated,
which is used to tint widgets -->
<item name="colorAccent">@color/colorAccent</item>
<!-- to hide white screen in start of window -->
<item name="android:windowIsTranslucent">true</item>
<item name="colorControlNormal">@color/orange_two</item>
<item name="colorControlActivated">@color/pumpkin_orange</item>
</style>
Your Mainfest
Your Mainfest
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"> // here is the style used
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
回答by opt05
EDIT 6/28/16:The below answer is no longer correct. See the accepted answer on the new way Google has allowed tinting on pre-v21 devices with the appcompat library.
EDIT 6/28/16:The below answer is no longer correct. See the accepted answer on the new way Google has allowed tinting on pre-v21 devices with the appcompat library.
Original Answer:
Original Answer:
The short answer is: no. Custom drawables will need to be created for use on pre-v21 devices. This is because the special tint aware widgets are currently hidden because they're an unfinished implementation detail at this time (which Google states that this may change in the future, according to their developer blogin the FAQ section)
The short answer is: no. Custom drawables will need to be created for use on pre-v21 devices. This is because the special tint aware widgets are currently hidden because they're an unfinished implementation detail at this time (which Google states that this may change in the future, according to their developer blogin the FAQ section)
There are two scenarios you could override the colorAccent that may work:
There are two scenarios you could override the colorAccent that may work:
- Have your own custom version of the widget (i.e. you've extended EditText)
- Creating the EditText without a LayoutInflater (i.e., calling new EditText()).
- Have your own custom version of the widget (i.e. you've extended EditText)
- Creating the EditText without a LayoutInflater (i.e., calling new EditText()).