简体   繁体   中英

How do I use my own colors in android XML

I have an XML file I created in the res/value folder and try to reference it by writing

android:background="@colors/red"

and it does not work.

I also tried placing this file in the drawable folder but to no avail.

Refer to color resources with @color/name , not @colors/name .

For example, in res/values/whatever.xml :

<color name="your_color_name">#12345678</color>

Then in a layout xml:

android:background="@color/your_color_name"
  • Your color res file has to be under res/values/ (You may add it to a qualifier res/values folder though)
  • Add the color in this format:

     <resources> <color name="white">#ffffff</color> </resources> 
  • Refer to it using @color/white .

You can declare a color in your color.xml file like below,

<color name="aqua_blue">#6495ED</color>

And then use it below,

android:background="@color/aqua_blue"

简单的方法

android:background="#8EE8B5"
<?xml version="1.0" encoding="utf-8"?>
<resources>
 <color name="white">#FFFFFF</color>
 <color name="yellow">#FFFF00</color>
 <color name="fuchsia">#FF00FF</color>
 <color name="red">#FF0000</color>
 <color name="silver">#C0C0C0</color>
 <color name="gray">#808080</color>
 <color name="olive">#808000</color>
 <color name="purple">#800080</color>
 <color name="maroon">#800000</color>
 <color name="aqua">#00FFFF</color>
 <color name="lime">#00FF00</color>
 <color name="teal">#008080</color>
 <color name="green">#008000</color>
 <color name="blue">#0000FF</color>
 <color name="navy">#000080</color>
 <color name="black">#000000</color>
</resources>

make one xml named colors.xml in res and add color names and codes as you want custum

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM