简体   繁体   中英

Android Color Drawable Resource

I have the following colors:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <drawable name="darkgray">#404040ff</drawable>
  <drawable name="black">#000ff</drawable>
  <drawable name="red">#ff00ff</drawable>
  <drawable name="green">#0ff0ff</drawable>
  <drawable name="lightgray">#c0c0c0ff</drawable>
  <drawable name="white">#ffffffff</drawable>
  <drawable name="yellow">#ffff0ff</drawable>
  <drawable name="blue">#00ffff</drawable>
  <drawable name="gray">#808080ff</drawable>
  <drawable name="magenta">#ff0ffff</drawable>
  <drawable name="cyan">#0ffffff</drawable>
</resources>

And I have the following in my Main Layout file for a Button background color: (snippet)

<Button
  android:id="@+id/widget27"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/yellow"
  android:text="Button"
  android:layout_x="30px"
  android:layout_y="102px"
>
</Button>

My question is: What do I name the file with the colors, and where do I put it? I am getting a compile error, aresgen exit error.

I'm looking for a filename and location where the background colors can be accessed.

Thanks,

You should create an XML file at in res/values/colors.xml -- store all your colors in here.

Additionally, you should read this whole page, but here is the section that directly relates to your question:

http://developer.android.com/guide/topics/resources/more-resources.html#Color

First of all you have forgotten, that Button is closed like this

<Button
  android:id="@+id/widget27"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@drawable/yellow"
  android:text="Button"
  android:layout_x="30px"
  android:layout_y="102px"
/>

and next you can use this instead of your code

<Button
  android:id="@+id/widget27"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@android:color/white"
  android:text="Button"
  android:layout_x="30px"
  android:layout_y="102px"
/>

but if you want to use your color use this link

http://android-er.blogspot.com/2010/03/using-color-in-android.html

try this on the xml

<color name="white">#FFFFFF</color>

and on your button

<Button
  android:id="@+id/widget27"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@color/white"
  android:text="Button"
  android:layout_x="30px"
  android:layout_y="102px"
/>

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