简体   繁体   English

如何指定具有attr属性的可绘制源?

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

I am writing an xml for a drawable. 我正在为可绘制对象编写xml。 My question is: is there a way to give the src property a reference to a attribute defined in the attr.xml file ? 我的问题是:有没有办法给src属性提供对attr.xml文件中定义的属性的引用? I've tried: 我试过了:

android:src="?image"

and

android:src="?attr/image"

None of them seem to work. 他们似乎都不起作用。 Here's my attr.xml: 这是我的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. 我有几次遇到在Eclipse构建中不更新XML更改的经历。
  2. Are you sure that your application or activity is either using HOLO or HOLO_LIGHT theme? 您确定您的应用程序或活动正在使用HOLO或HOLO_LIGHT主题吗? Check the 'android:theme' key is set in your activity. 检查您的活动中是否设置了“ android:theme”键。
  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. 尝试使用'android:background'属性代替'android:src'属性,这意味着不要使用ImageView而是仅使用View或LinearLayout的背景。

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 然后在你的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 然后在您的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" ... />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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