在 android Lollipop 错误中以编程方式为 CheckBox 和 RadioButton 设置主题
Programmatically theming CheckBox and RadioButton in android Lollipop bug
我正在做一个项目,我必须以编程方式为复选框和单选按钮设置主题。
除 Lollipop 外,一切正常,只有当我以编程方式将所述按钮设置为选中时,按钮的状态才会反转
https://drive.google.com/file/d/0B6JcrJ_vY1HnOXF6Yk4yT29ZUTA/view?usp=sharing
正如您在视频中看到的那样(对质量表示抱歉),当我单击它们时,按钮可以正常工作,但是当我使用 "everything" 按钮以编程方式设置它们时,它们只会在棒棒糖上出现异常。
这是着色代码
public static void setupCheckBox(AppCompatCheckBox checkBox) {
int colorOn = getCurrentAccentColor();
int colorOff = ContextCompat.getColor(checkBox.getContext(), R.color.grey);
int colorDisabled = 0xFFAAAAAA;
ColorStateList buttonStates = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_checked},
new int[]{-android.R.attr.state_enabled},
new int[]{}
},
new int[]{
colorOn,
colorDisabled,
colorOff
}
);
checkBox.setSupportButtonTintList(buttonStates);
}
答案在这里
https://issuetracker.google.com/issues/37084519#comment10
AppCompatRadioButton 不适合那样使用。您使用的是私有 API,如果使用不当,它必然会中断。
使用CompoundButtonCompat.setButtonTintList()。
我正在做一个项目,我必须以编程方式为复选框和单选按钮设置主题。
除 Lollipop 外,一切正常,只有当我以编程方式将所述按钮设置为选中时,按钮的状态才会反转
https://drive.google.com/file/d/0B6JcrJ_vY1HnOXF6Yk4yT29ZUTA/view?usp=sharing
正如您在视频中看到的那样(对质量表示抱歉),当我单击它们时,按钮可以正常工作,但是当我使用 "everything" 按钮以编程方式设置它们时,它们只会在棒棒糖上出现异常。
这是着色代码
public static void setupCheckBox(AppCompatCheckBox checkBox) {
int colorOn = getCurrentAccentColor();
int colorOff = ContextCompat.getColor(checkBox.getContext(), R.color.grey);
int colorDisabled = 0xFFAAAAAA;
ColorStateList buttonStates = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_checked},
new int[]{-android.R.attr.state_enabled},
new int[]{}
},
new int[]{
colorOn,
colorDisabled,
colorOff
}
);
checkBox.setSupportButtonTintList(buttonStates);
}
答案在这里 https://issuetracker.google.com/issues/37084519#comment10
AppCompatRadioButton 不适合那样使用。您使用的是私有 API,如果使用不当,它必然会中断。
使用CompoundButtonCompat.setButtonTintList()。