3

因此,android 在中定义了以下内容themes.xml

<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:我有一个类似的问题,我还没有找到答案。有人可以帮忙吗!

4

1 回答 1

-1

也许您还需要在文件 res/values/attrs.xml 中声明属性引用:

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

并将其?colorPressedHighlight称为?android:colorPressedHighlight.

于 2013-04-13T22:26:00.147 回答