自定义列表适配器,访问android.R资源

时间:2013-03-25 22:22:00

标签: android listview

我正在尝试以编程方式为列表项创建一个视图 - 它需要类似于原始的android复选框模板 - 这里是android原始模板xml:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/textCheckMark"
android:paddingLeft="6dip"
android:paddingRight="6dip"
/>

这是我的来源:

CheckedTextView ctv = new CheckedTextView(context);
ctv.setId(CTV_ID);
ctv.setCheckMarkDrawable(android.R.attr.textCheckMark);
ctv.setPadding(Utils.toDP(4, context), Utils.toDP(4, context), Utils.toDP(4, context), Utils.toDP(4, context));

问题出在setCheckMarkDrawable行,我得到例外:

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

显然我的代码无法访问此资源。 我该怎么做,我做错了什么,如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

好的,找到了解决方案:

TypedValue typedValue = new TypedValue();
context.getTheme().resolveAttribute(android.R.attr.textCheckMark, typedValue, true);
return typedValue.resourceId; // <- This is the actual resource id to be used

似乎需要将此值解析为当前主题资源ID。 类似的问题:background issue with styles and themes

相关问题