[英]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.