=
先看一下一个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>
本文转自zdd博客园博客,原文链接:http://www.cnblogs.com/graphics/p/4704863.html,如需转载请自行联系原作者