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我還沒有找到答案。有人可以幫助!