书法字典:https://www.shufadict.com

在XML drawable中引用自定义颜色

==

先看一下一个xml drawable文件,这是一个button的样式。

<?xml version="1.0" encoding="utf-8" ?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#FB7C16" />
            <stroke
                android:width="1dp"
                android:color="#FB7C16" />
            <corners
                android:radius="3dp" />
        </shape>
    </item>
    <item>
        <shape>
            <solid
                android:color="#1FAC75"/>
            <stroke
                android:width="1.0dp"
                android:color="#1FAC75" />
            <corners
                android:radius="3dp" />
        </shape>
    </item>
</selector>

可以看到这里好几处用到了颜色,但是都是直接指定颜色值,如果我想引用一个外部颜色,比如定义在values/refs里面的。那该怎么办呢?

先在values/refs.xml文件里面定义颜色,如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <drawable name="whitecolor">#FFFFFF</drawable>
    <drawable name="blackcolor">#000000</drawable>
    <drawable name="graycolor">#aaaaaa</drawable>
    <drawable name="drawerdivider">#FFFFFF</drawable>
    <!--<drawable name="theme_color">#1FAC75</drawable>-->
    <drawable name="theme_color">#FF3300</drawable>
    <drawable name="search_page_color">#60D1A3</drawable>
    <drawable name="grid_background">#FFFDF1</drawable>
    <!--<color name="theme_color">#1FAC75</color>-->
    <color name="theme_color">#FF3300</color>
</resources>

然后在drawable xml文件里面引用颜色即可,如下:

    </item>
    <item>
        <shape>
            <solid
                android:color="@color/theme_color"/>
            <stroke
                android:width="1.0dp"
                android:color="@color/theme_color" />
            <corners
                android:radius="3dp" />
        </shape>
    </item>
</selector>

 

==

posted on 2015-08-05 15:38  翰墨小生  阅读(9544)  评论(0编辑  收藏  举报

导航

书法字典:https://www.shufadict.com