问题
in attrs I have
<attr name="bzz" format="color" />
then in theme
<style name="mytheme" parent="android:Theme">
<item name="bzz">@color/aaa</item>
</style>
and in the code this works great
tv.setBackgroundResource(R.color.aaa);
but when I do this it gives me an error
tv.setBackgroundResource(R.attr.bzz);
I do not understand what is the problem, my logic is that I set the bzz as reference to color so that should work fine, but it does not :)
it says like android.content.res.Resources$NotFoundException: Resource ID #0x7f010008
but I do not understand what resource can't be found ?
I am sure that the color is there sins if I set it directly it works great, what exacly is the thing that is not linked correctly
Thanks
回答1:
You need to resolve the attr
to get the corresponding color
's resource id. Then you can set the TextView
's background resource to the obtained resource id.
Example code:
TypedValue typedValue = new TypedValue();
getTheme().resolveAttribute(R.attr.bzz, typedValue, true);
tv.setBackgroundResource(typedValue.resourceId);
来源:https://stackoverflow.com/questions/7675231/background-issue-with-styles-and-themes