CheckBox上的API21 setButtonTintList

bee*_*ezn 8 android android-checkbox android-5.0-lollipop

我正在尝试使用新的Android Lollipop API setButtonTintList()以编程方式将颜色应用于android CheckBoxes.

我已经设置了以下基本ColorStateListcheckbox_color.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"
          android:color="@color/red" />

    <item android:state_checked="false"
          android:color="@color/green" />

</selector>
Run Code Online (Sandbox Code Playgroud)

Button在我的应用程序中的特定点击我膨胀一个新的布局,其中包含一个CheckBox到我的主布局(mLayoutTotalItemRow)通过:

View itemRow = getActivity().getLayoutInflater().inflate(R.layout.row_add_purchase, mLayoutTotalItemRow, false);
Run Code Online (Sandbox Code Playgroud)

通胀后,我想申请checkbox_color.xml新的CheckBox通道(我想在代码而不是xml中执行此操作的原因是我想根据一些变量设置不同的颜色):

mCheckBoxEnabled.setButtonTintList(mContext.getResources().getColorStateList(R.color.checkbox_color));
Run Code Online (Sandbox Code Playgroud)

现在出现一个奇怪的事情,当CheckBox它第一次创建时,它是红色的(因此state_checked="true".一旦我取消选中它,它就是绿色(state_chacked="false").但是当我现在再次检查它时,它会变为勾选但它保持绿色!因此不知何故内部视图状态未更改为"已检查".

我的复选框xml:

   <CheckBox
    android:id="@+id/cb_item_enabled"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/key_line_1_minus_checkbox_margin"
    android:layout_marginEnd="@dimen/key_line_2_minus_key_line_1_minus_checkbox"
    android:layout_gravity="center_vertical"
    android:checked="true"/>
Run Code Online (Sandbox Code Playgroud)

我在这里做错了什么想法?

Kev*_*tel 6

我已经向Android项目报告了这个错误. 问题157166
修复下一个版本5.1

这已在5.1中修复,尚未完全发布.

状态:FutureRelease 

编辑10.03.2015

发布于5.1.