【问题标题】:Change Android CheckBox box's color programmatically (support library)?以编程方式更改 Android CheckBox 框的颜色(支持库)?
【发布时间】:2017-06-04 17:46:39
【问题描述】:

我正在尝试以编程方式将复选框的颜色更改为与主题默认颜色不同的颜色。问题是我正在做这样的事情:

checkbox.setSupportButtonTintList(ColorStateList);

它有效,但根据其类文档,此方法似乎仅限于同一包 (com.android.support) 中的类使用。这是我从 Android Studio 收到的警告:

AppCompatCheckBox.setSupportButtonTintList can only be called from within the same library group (groupId=com.android.support)

是否有针对所有 API 级别执行此操作的标准/正确方法?

【问题讨论】:

    标签: android checkbox android-support-library


    【解决方案1】:

    最后,从一位 Google 人员那里找到了答案:https://code.google.com/p/android/issues/detail?id=202235。我没有使用是对的:

    checkbox.setSupportButtonTintList(ColorStateList);
    

    这似乎是一个私有 API。相反,您必须使用:

    CompoundButtonCompat.setButtonTintList(checkbox, colorStateList);
    

    【讨论】:

    • 谢谢!还有一个有用的提示:要以编程方式创建 ColorStateList,您可以使用 ContextCompat.getColorStateList(context, R.color.my_checkbox_selector)
    【解决方案2】:

    根据rylexr的回答,你可以通过以下方式指定颜色:

    CompoundButtonCompat.setButtonTintList(checkboxView, ColorStateList
                            .valueOf(getResources().getColor(R.color.red)));
    

    【讨论】:

      【解决方案3】:
      chxAll.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#CC0000")));
      

      chxAll 是android.widget.CheckBox 的对象

      替换指定颜色的hexacolor代码

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-02
        • 2015-12-05
        • 1970-01-01
        • 1970-01-01
        • 2023-03-08
        • 2019-11-23
        • 2015-12-06
        • 2021-03-31
        相关资源
        最近更新 更多