gpt4 book ai didi

android - 无法在自定义颜色选择器中使用标准的 android 颜色属性

转载 作者:行者123 更新时间:2023-11-29 14:16:06 24 4
gpt4 key购买 nike

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

最佳答案

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

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

并将其称为 ?colorPressedHighlight 而不是 ?android:colorPressedHighlight

关于android - 无法在自定义颜色选择器中使用标准的 android 颜色属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11229207/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com