I am writing an xml for a drawable. My question is: is there a way to give the src property a reference to a attribute defined in the attr.xml file ? I've tried:
android:src="?image"
and
android:src="?attr/image"
None of them seem to work. Here's my attr.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="image" format="reference"></attr>
</resources>
and where i define the values, which are related to theme:
<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>
A few suggestions:
BTW, the correct syntax to reference your attribute is:
<... android:src="?attr/image" .../>
4th option: If all else fails, you can try using the attribute as a style. (just a hunch, it may not work) eg:
<resources>
<attr name="imageStyle" format="reference"></attr>
</resources>
then in your 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>
then in your themes.xml
<style name="HOLO" ...>
<item name="imageStyle">@style/DarkImageStyle</item>
</style>
<style name="HOLO_LIGHT" ....>
<item name="imageStyle">@style/LightImageStyle</item>
</style>
finally in your layout:
<ImageView style="?attr/imageStyle" ... />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.