2012-06-27 90 views
2

由此可見,Android定義themes.xml如下:不能在自定義顏色選擇使用Android標準顏色屬性

<style name="Theme"> 
    ... 
    <item name="colorPressedHighlight">@color/legacy_pressed_highlight</item> 
</style> 

和:

<style name="Theme.Holo"> 
    ... 
    <item name="colorPressedHighlight">@color/holo_blue_light</item> 
</style> 

我想用這個colorPressedHighlight作爲背景色爲我按下時自定義Button。所以我定義在res/color/app_button_background.xml如下:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item 
     android:state_pressed="true" 
     android:drawable="?android:colorPressedHighlight"/> 
    <item android:drawable="@android:color/transparent" /> 
</selector> 

最後,我定義我的自定義ImageButton風格:

<style name="App_ImageButtonStyle" parent="@android:style/Widget.ImageButton"> 
    <item name="android:gravity">center</item> 
    <item name="android:background">@color/app_button_background</item> 
</style> 

我在應用啓動時崩潰與以下調用堆棧:

06-27 20:24:41.954: E/AndroidRuntime(532): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #6: <item> tag requires a 'drawable' attribute or child tag defining a drawable 
06-27 20:24:41.954: E/AndroidRuntime(532): at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:178) 
06-27 20:24:41.954: E/AndroidRuntime(532): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:867) 
06-27 20:24:41.954: E/AndroidRuntime(532): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:804) 
06-27 20:24:41.954: E/AndroidRuntime(532): at android.content.res.Resources.loadDrawable(Resources.java:1920) 

我知道直接訪問@color/legacy_pressed_highlight@color/holo_blue_light而不是通過colorPressedHighlight訪問它們修復了崩潰,但是它不能解決問題。主題可能會有所不同,因此我需要通過colorPressedHighlight屬性訪問它。 PS:我有類似的problem我還沒有找到答案。有人可以幫助!

回答

-1

或許你還需要在文件RES /價值/ attrs.xml申報屬性引用:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <attr name="colorPressedHighlight" format="reference" /> 
</resources> 

而且稱其爲?colorPressedHighlight不是作爲?android:colorPressedHighlight