gpt4 book ai didi

android - 复选框上的 API21 setButtonTintList

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:52:24 25 4
gpt4 key购买 nike

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

我在 checkbox_color.xml 中设置了以下基本 ColorStateList:

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

在我的应用程序中单击特定的 Button 时,我通过以下方式将包含 CheckBox 的新布局膨胀到我的主布局 (mLayoutTotalItemRow) 中:

View itemRow = getActivity().getLayoutInflater().inflate(R.layout.row_add_purchase, mLayoutTotalItemRow, false);

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

mCheckBoxEnabled.setButtonTintList(mContext.getResources().getColorStateList(R.color.checkbox_color));

现在发生了一件奇怪的事情,当第一次创建 CheckBox 时,它是红色的(因此 state_checked="true"。一旦我取消选中它,它就是绿色(state_chacked="false")。但是当我现在再次检查它时,它变为勾选但它保持绿色!因此内部 View 状态不知何故没有变为“已选中”。

我的复选框 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"/>

知道我在这里做错了什么吗?

最佳答案

我已将错误报告给 Android 项目。 Issue 157166
修复下一个版本 5.1

This is fixed in 5.1, which isn't quite released yet.

Status: FutureRelease 

编辑 10.03.2015

Released in 5.1.

关于android - 复选框上的 API21 setButtonTintList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28047291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com