[英]How can i specify a drawable source with an attr attribute?
我正在为可绘制对象编写xml。 我的问题是:有没有办法给src属性提供对attr.xml文件中定义的属性的引用? 我试过了:
android:src="?image"
和
android:src="?attr/image"
他们似乎都不起作用。 这是我的attr.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="image" format="reference"></attr>
</resources>
以及在哪里定义与主题相关的值:
<resources>
<style name="HOLO" parent="@android:style/Theme.Black">
<item name="android:textColor">#00FFFF</item>
<item name="image">@drawable/holo_vcard_settings_icon</item>
</style>
<style name="HOLO_LIGHT" parent="@android:style/Theme.Light">
<item name="android:textColor">#FF00FF</item>
<item name="image">@drawable/holo_light_vcard_settings_icon</item>
</style>
</resources>
一些建议:
顺便说一句,引用您的属性的正确语法是:
<... android:src="?attr/image" .../>
第四个选项:如果所有其他方法均失败,则可以尝试使用属性作为样式。 (只是预感,可能不起作用),例如:
<resources>
<attr name="imageStyle" format="reference"></attr>
</resources>
然后在你的styles.xml中
<style name="LightImageStyle">
<item name="android:src">@drawable/light_png</item>
</style>
<style name="DarkImageStyle">
<item name="android:src">@drawable/dark_png</item>
</style>
然后在您的themes.xml中
<style name="HOLO" ...>
<item name="imageStyle">@style/DarkImageStyle</item>
</style>
<style name="HOLO_LIGHT" ....>
<item name="imageStyle">@style/LightImageStyle</item>
</style>
终于在您的布局中:
<ImageView style="?attr/imageStyle" ... />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.