gpt4 book ai didi

以编程方式设置 android.R.attr.listChoiceIndicatorMultiple 时的 android.content.res.Resources$NotFoundException

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:34:40 26 4
gpt4 key购买 nike

我正在尝试以编程方式为 ListView 中的 CheckedTextView 项目设置“android:checkMark”属性。运行我的应用程序时出现以下异常:

android.content.res.Resources$NotFoundException: Resource ID #0x101021a

ID #0x101021a 的资源对应于 android.R.attr.listChoiceIndicatorMultiple,这正是我传递给我的 CheckedTextView 的值:

mCheckedTextView.setCheckMarkDrawable(android.R.attr.listChoiceIndicatorMultiple)

这不是从 Java 中实现的方法吗?我尝试(并成功)从 XML 布局触发所需的行为:

<CheckedTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:id="@android:id/text1" />

问题是我在编译时不知道它是否应该

android:checkMark="?android:attr/listChoiceIndicatorMultiple"

android:checkMark="?android:attr/listChoiceIndicatorSingle"

因此,我需要在运行时设置这些值。

最佳答案

我猜想以编程方式设置属性引用而不是Drawable引用是问题所在。

在这种情况下,android.R.attr.listChoiceIndicatorMultiple corresponds to android.R.drawable.btn_check,因此您可以尝试设置它。


或者,如果您可以获得属性,您可以调用 getDrawable()TypedArray 上动态获取 Drawable 值。

编辑:
由于listChoiceIndicatorMultiple的值取决于当前主题,所以需要询问当前主题来解析引用:

int[] attrs = { android.R.attr.listChoiceIndicatorMultiple };
TypedArray ta = getContext().getTheme().obtainStyledAttributes(attrs);
Drawable indicator = ta.getDrawable(0);
view.setCheckMarkDrawable(indicator);
ta.recycle();

一定要缓存可绘制对象,而不是对 ListView 中的每个项目都执行此操作。

这只是一个非常基本的示例,但它适用于默认主题。如果您有自定义主题,我不确定需要做什么才能完全解析 attrs

关于以编程方式设置 android.R.attr.listChoiceIndicatorMultiple 时的 android.content.res.Resources$NotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4783002/

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