background issue with styles and themes

雨燕双飞 提交于 2019-12-19 08:44:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!