gpt4 book ai didi

android - -Tag 中的颜色引用,位于

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:18:27 25 4
gpt4 key购买 nike

我对在 layer-list drawable 中使用引用有疑问。

我想在我的应用中使用自定义按钮,由 layer-list 组成。

  • 这是按钮的最终可绘制 btn.xml,由 选择器 组成:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/btn_normal"/>
</selector>
  • 按下状态可绘制 btn_pressed 看起来像这样:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<solid android:color="?custom_color" />
</shape>
</item>
<item android:drawable="@drawable/btn_normal"/>
</layer-list>

重要的部分是行,我在其中声明颜色是对 custom_color 的引用,我定义如下。

  • 属性.xml:
<resources>
<attr name="custom_color" format="reference|color" />
</resources>
  • 主题.xml:
<resources>
<style name="MyTheme" parent="android:Theme.Light.NoTitleBar">
<item name="custom_color">#ff33b5e5</item>
</style>
</resources>

Eclipse 完全没有给我任何错误,而且它编译得很好。但是当我在我的 ICS Nexus S 或模拟器(无论什么版本)上启动应用程序时,它崩溃了。 Logcat 指出以下内容:

03-23 14:33:38.832: E/AndroidRuntime(636): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/btn.xml from drawable resource ID #0x7f020006

[...]

03-23 14:33:38.832: E/AndroidRuntime(636): Caused by: android.content.res.Resources$NotFoundException: File res/drawable/btn_pressed.xml from drawable resource ID #0x7f020009

如果我注释掉 layer-list 中的 shape 元素,或设置硬编码颜色,一切正常。所以在层列表中引用属性似乎有问题。

那么,有人知道这个问题的解决方案吗?我想根据我的应用程序使用的主题更改该图层列表中的颜色。

最佳答案

好吧,看来这是一个直到 Android L 才被关注的错误。
有关 Android 问题跟踪器的更多详细信息:Issue 26251

它应该在 Android L 中修复,但至少在模拟器中使用 Android L Preview 进行测试它似乎还不能完全工作。至少它不会使应用程序崩溃,但它没有显示正确的颜色,而是给我透明度 (#00ffffff)。也许设备和/或 Android L 稳定版发布后情况并非如此。

关于android - <item>-Tag 中的颜色引用,位于 <layer-list> 内,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9840386/

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