在绘制不能使用引用的颜色(主题) [英] Can't use referenced color (by theme) in drawable

查看:209
本文介绍了在绘制不能使用引用的颜色(主题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑:

styles.xml

 <样式名称=BlueTheme父=@安卓风格/ Theme.Black.NoTitleBar>
    <项目名称=theme_color> @彩色/ theme_color_blue< /项目>
< /风格>

attrs.xml

 < attr指示NAME =theme_color格式=参考/>

color.xml

 <颜色名称=theme_color_blue>#ff0071d3< /彩色>

终于在绘制progress_bar.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<层列表的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>    <项目机器人:ID =@机器人:ID /背景>
        <形状和GT;
            <中风
                机器人:宽=2px的
                机器人:颜色=#ff515151/>            [固体机器人:色=@色/透明/>
        < /形状>
    < /项目>
    <项目机器人:ID =@机器人:ID /进度>
        <夹>
            <形状和GT;
                [固体机器人:色= />中ATTR / theme_color?
            < /形状>
        < /夹>
    < /项目>< /层列表>

使用进度条绘制时

 <进度
    机器人:ID =@ + ID / progressBarStartup
    风格=机器人:ATTR / progressBarStyleHorizo​​ntal
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:重力=CENTER_HORIZONTAL
    机器人:progressDrawable =@绘制/ progress_bar
    机器人:知名度=看得见/>

我得到一个运行时错误即可。看来Android的不能得到的颜色主题。

  06-24 16:35:57.032:E / AndroidRuntime(3493):了java.lang.RuntimeException:无法启动活动ComponentInfo {} xxx.xxx.activities.startup.ActivityStartup :android.view.InflateException:二进制XML文件行#33:错误充气类android.widget.ProgressBar
06-24 16:45:32.265:E / AndroidRuntime(9901):android.content.res.Resources $ NotFoundException:由造成资源不是绘制对象(彩色或路径):{的TypedValue T = 0X2 / D = 0x7f010000一个= -1}


解决方案

是的,这似乎是一个已知的问题。但是有一个解决方法。而不是使用的?ATTR 引用的颜色,如你所说,可以创建多个可绘制(多达你有不同的颜色),让每个直接调用它的颜色。在主题层面,引用自己取决于主题可绘制。它的工作原理的方式。

更新:

在棒棒糖,你可以在着色的XML绘制。为了支付pre-棒棒糖,你可以简单地在质疑称此为您所有的可绘制(多亏了应用程序兼容性库的更新版本):

 公共无效setDrawableColor(可绘制D,INT颜色){
  如果(Build.VERSION.SDK_INT< Build.VERSION_ codeS.LOLLIPOP){
    D = DrawableCompat.wrap(D);
    DrawableCompat.setTint(d.mutate(),彩色);
  }
}

Consider:

styles.xml

<style name="BlueTheme" parent="@android:style/Theme.Black.NoTitleBar">
    <item name="theme_color">@color/theme_color_blue</item>
</style>

attrs.xml

<attr name="theme_color" format="reference" />

color.xml

<color name="theme_color_blue">#ff0071d3</color>

and finally the drawable progress_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/background">
        <shape>
            <stroke
                android:width="2px"
                android:color="#ff515151" />

            <solid android:color="@color/transparent" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="?attr/theme_color" />
            </shape>
        </clip>
    </item>

</layer-list>

when using progress bar drawable:

<ProgressBar
    android:id="@+id/progressBarStartup"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:progressDrawable="@drawable/progress_bar"
    android:visibility="visible" />

I get a runtime error. It seems Android can't get the color theme.

06-24 16:35:57.032: E/AndroidRuntime(3493): java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx.xxx.activities.startup.ActivityStartup}: android.view.InflateException: Binary XML file line #33: Error inflating class android.widget.ProgressBar


06-24 16:45:32.265: E/AndroidRuntime(9901): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f010000 a=-1}

解决方案

Yes, this seems to be a known problem. But there is a workaround. Instead of using ?attr referenced colors like you described, create several drawables (as many as the different colors you have), let each call its colors directly. On the theme level, reference to the drawables themselves depending on the theme. It works that way.

Update:

On Lollipop, you can tint the drawable in XML. To cover pre-Lollipop, you can simply call this for all your drawables in question (thanks to the newer versions of the appcompat library):

public void setDrawableColor(Drawable d, int color) {
  if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
    d = DrawableCompat.wrap(d);
    DrawableCompat.setTint(d.mutate(), color);
  }
}

这篇关于在绘制不能使用引用的颜色(主题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆