【问题标题】:How to apply tint to checkbox preference programmatically in Android?如何在Android中以编程方式将色调应用于复选框首选项?
【发布时间】:2020-03-14 23:29:07
【问题描述】:

如何在 Android 中以编程方式将色调应用于 CheckBox 首选项?

由于主题原因,CheckBox 首选项已显示为灰色,但我需要以编程方式将色调应用/更改为其他颜色。如何做到这一点?

【问题讨论】:

    标签: android


    【解决方案1】:

    我建议@ywwynm 回答来自 How to change checkbox checked color programmatically

    public static void setCheckBoxColor(AppCompatCheckBox checkBox, int uncheckedColor, int checkedColor) {
        ColorStateList colorStateList = new ColorStateList(
                new int[][] {
                        new int[] { -android.R.attr.state_checked }, // unchecked
                        new int[] {  android.R.attr.state_checked }  // checked
                },
                new int[] {
                        uncheckedColor,
                        checkedColor
                }
        );
        checkBox.setSupportButtonTintList(colorStateList);
    }
    

    setSupportButtonTintList 设置为ColorStateList 然后以编程方式设置选中和未选中状态肯定会得到您的答案

    【讨论】:

      【解决方案2】:

      Tinting Checkbox on pre v21

      可以通过使用主题属性并设置 colorControlNormal 和 colorControlActivated 来为复选框着色:

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

      【讨论】:

        猜你喜欢
        • 2011-09-02
        • 2012-06-17
        • 1970-01-01
        • 2016-12-22
        • 2016-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-24
        相关资源
        最近更新 更多