设置View的背景基于主题属性崩溃的应用程序 [英] Setting a View background based on theme attribute crash the app
问题描述
我试图设置基于当前主题属性一个ListView背景颜色,但它崩溃每次在ListView是shown.It好像我做错了什么,但我不能看到什么...
I'm trying to set a ListView background color based on the current theme attribute, but it crash every time the ListView is shown.It seems I'm doing something wrong but I can't see what...
下面是我在做什么:
首先,创建背景色:
<resources>
<color name="userlist_background_light">#fff0f0f0</color>
<color name="userlist_background_dark">#ff040404</color>
</resources>
二,我的自定义主题创建属性:
Second, create attributes for my custom themes:
<resources>
<attr name="userlist_background" format="reference|color" />
</resources>
三,在我的主题设置此属性:
Third, setting this attribute in my themes:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Light" parent="Theme.Sherlock.Light">
<item name="userlist_background">@color/userlist_background_light</item>
</style>
<style name="Dark" parent="Theme.Sherlock">
<item name="userlist_background">@color/userlist_background_dark</item>
</style>
</resources>
最后,在ListView XML使用这个属性:
And finally, using this attribute in the ListView xml:
<ListView
android:id="@+id/user_bar"
android:layout_width="0dip"
android:layout_height="0dip"
android:background="?attr/userlist_background"
android:cacheColorHint="?userlist_background"
android:visibility="gone" />
即使是Eclipse的布局来看崩溃。当然,如果我的背景属性使用@色/直接正常工作。 ?:ATTR / colorBackground Android的它,即使我用说的,工作。
Even the Eclipse layout view crash. Of course, it works fine if I use a "@color/" directly in the background attribute. It even work if I use say, "?android:attr/colorBackground".
消息的错误是:
android.view.InflateException:二进制XML文件行#8:错误
充气类android.view.ListView产生的原因:
android.content.res.Resources $ NotFoundException:资源不是
可绘制(彩色或路径):{的TypedValue T = 0X2 / D = 0x7f010068一个= -1}
android.view.InflateException: Binary XML file line #8: Error inflating class android.view.ListView Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f010068 a=-1}
我是pretty肯定,我做错了什么,因为它与Android属性的作品,但我不能在我的谷歌搜索来查找。
I'm pretty sure I'm doing something wrong, as it works with android attributes, but I haven't be able to find what during my Google searches.
我希望你能帮帮我!
非常感谢,
塞巴斯蒂安。
推荐答案
好吧,我固定它,它一个错误是由于!
Ok, I fixed it, and it was due to a mistake!
我有两个的themes.xml
文件,一个用于蜂窝+,一个用于Gingerbread-。我只添加了新的属性到的themes.xml
目标Gingerbread-,并在ICS被测试。
I have two themes.xml
files, one for Honeycomb+, and one for Gingerbread-. I've only added the new attributes to the themes.xml
targeting Gingerbread-, and was testing on ICS.
也许这会帮助别人谁就会犯同样的错误!
Maybe it'll help others who'll make the same mistake!
这篇关于设置View的背景基于主题属性崩溃的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!