• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

使用属性修改可绘制元素颜色

用户头像
it1352
帮助1

问题说明

我尝试使用属性制作自定义Drawable,但出现膨胀错误

I try to make a custom Drawable by using attribute but I have an inflating error

01-21 11:01:06.171: E/AndroidRuntime(13695): Caused by: android.content.res.Resources$NotFoundException: 
File res/drawable/action_bar_background.xml from drawable resource ID #0x7f020000

我的 action_bar_background.xml

My action_bar_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
 android:shape="rectangle" >
    <gradient
        android:angle="90"
        android:endColor="?attr/ActionBarEndColor"
        android:startColor="?attr/ActionBarStartColor" 
    />
</shape>

我的 attr.xml

My attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <attr name="ActionBarStartColor" format="reference|color" />
     <attr name="ActionBarEndColor" format="reference|color" />
</resources>

我的主题.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="Theme.Theme1" parent="@style/Theme.GreenDroid.Light.NoTitleBar">
        <item name="ActionBarStartColor">@color/greyDark</item>
        <item name="ActionBarEndColor">@color/greyLight</item>
    </style>

    <style name="Theme.Theme2" parent="@style/Theme.GreenDroid.Light.NoTitleBar">
        <item name="ActionBarStartColor">@color/myRed</item>
        <item name="ActionBarEndColor">@color/myWhite</item>
    </style>
</resources>

我不明白为什么,我的属性是引用或颜色,如果我在 themes.xml 中强制"颜色,它也不起作用.

I don't undestand why, my attribute is a reference or a color, if I "force" the color in themes.xml, it don't work too.

有人可以帮忙吗?

我发现了这个问题 但它不能解决我的问题.

I've found this question but it's not resolve my problem.

正确答案

#1

大家好消息!从 Lollipop (21) 开始,您可以使用属性来引用颜色之类的东西,例如?attr/colorPrimary.

Good news everyone! Starting Lollipop (21) you can use attributes to reference stuff like colors e.g. ?attr/colorPrimary.

原答案:

据我所知你不能从 drawable 中引用样式属性,我在 Android 平台资源中找不到这样的用法 ($ANDROID_HOME/platforms/android-*/data/res/),我所看到的是,即使是渐变可绘制对象,每个可绘制对象都是在适当位置定义的,或者它们只是引用其他可绘制对象,例如颜色可绘制对象.

As far I know you cannot reference style attributes from drawables, I couldn't find an usage like that in Android platform resources ($ANDROID_HOME/platforms/android-*/data/res/), all I see is that every drawable even for gradient drawables are defined in place or they just reference other drawables like color drawables.

这是一个相关的错误报告(星标):

Here is a related bug report for this (star it):

https://code.谷歌.com/p/android/issues/detail?id=26251

您可以通过使用单独的 xml drawable 来解决此问题,如在以下位置解释的:

You can workaround this by using separate xml drawables like explained at:

https://stackoverflow.com/a/13471695/172670

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfeicj
系列文章
更多 icon
同类精品
更多 icon
继续加载