简体   繁体   中英

How can i specify a drawable source with an attr attribute?

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:

  1. Try clean your project. I've experience a few times where the XML changes are not updated in an Eclipse build.
  2. Are you sure that your application or activity is either using HOLO or HOLO_LIGHT theme? Check the 'android:theme' key is set in your activity.
  3. Try using the 'android:background' property instead of 'android:src', meaning don't use ImageView and just use a View or LinearLayout's background.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM