Tinting Checkbox on pre v21

前端 未结 5 1157
春和景丽
春和景丽 2020-12-09 15:59

So, I want to apply tint to AppCompat Checkbox.

Everything works fine on Lollipop:

android:buttonTint=\"@color/purple_FF4081\"

or t

相关标签:
5条回答
  • 2020-12-09 16:20

    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

    <style name="MyCheckBox" parent="Theme.AppCompat.Light">  
    <item name="colorControlNormal">@color/indigo</item>
    <item name="colorControlActivated">@color/pink</item>
    </style> 
    

    layout xml:

    <CheckBox  
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Check Box"
            android:theme="@style/MyCheckBox"/>
    
    0 讨论(0)
  • 2020-12-09 16:31

    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:

    <android.support.v7.widget.AppCompatCheckBox 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        app:buttonTint="@color/COLOR_HERE" />
    
    0 讨论(0)
  • 2020-12-09 16:33

    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:

    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 blog in the FAQ section)

    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()).
    0 讨论(0)
  • 2020-12-09 16:37

    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

        <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

    <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>
    
    0 讨论(0)
  • 2020-12-09 16:38

    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));
    }
    
    0 讨论(0)
提交回复
热议问题