【问题标题】:Tinting Checkbox on pre v21v21 之前的着色复选框
【发布时间】:2014-12-19 08:30:37
【问题描述】:

所以,我想对 AppCompat Checkbox 应用 tint。

在 Lollipop 上一切正常:

android:buttonTint="@color/purple_FF4081"

或者这样:

android:theme="@style/Theme.MyTheme.PurpleAccent"

但设置任何此参数都不会改变棒棒糖之前的任何内容。仅当我为应用程序主题设置 colorAccent 时才有效。但我不希望所有小部件都改变它们的外观,只需要一个复选框。 有没有办法在不设置彩色绘图的情况下做到这一点?

【问题讨论】:

    标签: android checkbox


    【解决方案1】:

    我已经尝试了所有答案,但只有这个对我有用,将属性 colorControlNormalcolorControlActivated 添加到整个活动(或应用程序)的基本样式从控制器中删除主题

    这里是例子

        <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>
    

    你的主宴

    <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>
    

    【讨论】:

      【解决方案2】:

      我需要以编程方式进行,经过一段时间的挖掘,我终于找到了这个解决方案(在 Kitkat 和 Marshmallow 上测试过),我会发布它以防它对某人有所帮助:

      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));
      }
      

      【讨论】:

      • 对我不起作用。无论我提供什么颜色,选中和未选中状态都始终为灰色。我按如下方式调用该函数:'setAppCompatCheckBoxColors(checkBox, R.color.simple_red, R.color.colorPrimary);'我使用 'AppCompatCheckBox checkBox = new AppCompatCheckBox(context);' 创建了复选框。
      • 您正在使用颜色资源 ID,您应该指定颜色本身,如下所示:'setAppCompatCheckBoxColors(checkBox, Color.YELLOW, Color.RED);'或在您的实例中像这样:'setAppCompatCheckBoxColors(checkBox, activity.getResources().getColor(R.color.simple_red), activity.getResources().getColor(R.color.colorPrimary));'
      • @jzeferino:您编辑了我的答案并更改了“int[][] states = new int[][]{new int[]{-android.R.attr.state_checked}, new int[ ]{android.R.attr.state_checked}};"到“int[][] states = new int[][]{new int[]{android.R.attr.state_checked}, new int[]{android.R.attr.state_checked}};”。此更改似乎使代码不再工作,您能解释一下更改吗?
      • 同时我将代码还原为我的原始答案
      • 有了新的支持库,你应该使用:CompoundButtonCompat.setButtonTintList(_checkbox, new ColorStateList(states, colors));
      【解决方案3】:

      编辑 6/28/16: 以下答案不再正确。有关 Google 允许使用 appcompat 库在 v21 之前的设备上进行着色的新方式,请参阅已接受的答案。


      原答案:

      简短的回答是:不。需要创建自定义可绘制对象以在 v21 之前的设备上使用。这是因为特殊的色彩感知小部件目前是隐藏的,因为它们目前是一个未完成的实现细节(谷歌表示这可能会在未来发生变化,according to their developer blog 在常见问题解答部分)

      有两种情况可以覆盖 colorAccent 可能有效:

      • 拥有您自己的自定义小部件版本(即您已扩展 编辑文本)
      • 在没有 LayoutInflater 的情况下创建 EditText (即,调用 new EditText())。

      【讨论】:

      • 我看到谷歌在 widget tinting 中说:你不需要做任何特别的事情来使这些工作,只需像往常一样在你的布局中使用这些控件,AppCompat 会做剩下的。
      • 是的,如果您在应用程序 style.xml 中定义 colorAccent,Android 将使用 AppCompat 覆盖配色方案。但正如我所说,目前它与当前的 AppCompat 是全有或全无。
      • 很遗憾,他们现在添加了在任何视图上设置主题的功能(例如,您可以反转复选框的主题以使用相反的颜色),但它没有t 似乎在应用程序兼容库中工作。另外,您可以设置按钮 tinit 但这仅适用于 21+ :(
      【解决方案4】:

      您可以直接在 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" />
      

      【讨论】:

      • 非常感谢您的回答:)
      • 它不起作用。我已经检查过了。 + AppCompat* 小部件如果不是动态创建的,则会自动应用。
      【解决方案5】:

      在引入 AppCompatActivity 和新的支持库 for reference (outlined beautifully here) 后,这一切都发生了变化/p>

      styles.xml

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

      布局xml:

      <CheckBox  
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:checked="true"
              android:text="Check Box"
              android:theme="@style/MyCheckBox"/>
      

      【讨论】:

      • 这是金子!正在寻找这个答案。顺便问一下,复选框的默认颜色是什么?如果我想让正常状态成为默认颜色,那会是什么?我试过“@null”,但没用。
      • 你应该得到一枚奖牌!
      • 不确定这应该是答案,因为我收到警告,这需要 API 21,而问题特别指出 pre 21。除非我做错了什么!
      • 希望那些不起作用的人尝试自定义图像,因为这不起作用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 2013-02-25
      相关资源
      最近更新 更多