Color Drawables

You can create a PaintDrawable object that is a rectangle of color, with optionally rounded corners. This element can be defined in any of the files inside res/values/.

Source file format: XML file requiring a <?xml version="1.0" encoding="utf-8"?> declaration, and a root <resources> element containing one or more <drawable> tags.

Resource source file location: res/values/colors.xml (File name is arbitrary; standard practice is to put the PaintDrawable items in the file along with the numeric color values.)

Compiled resource datatype: Resource pointer to a PaintDrawable.

Resource reference name:

• XML: @[package:]drawable/some_name (where some_name is the name of a specific resource) Syntax

<drawable name=color name>color value</drawable>

<drawable>

A valid color value.

• name - The name used in referring to this drawable. Example XML Declaration

The following code declares several color drawables.

<resources>

<drawable

name=

"solid

red">#f0 0</drawable>

<drawable

name=

"solid

blue">#0000ff</drawable>

<drawable

name=

"solid

green">#f0f0</drawable>

</resources>

Example Java code

// Assign a PaintDrawable as the background to // a TextView on the current screen.

Drawable redDrawable = Resources.getDrawable(R.drawable.solid_red); TextView tv = (TextView)findViewByID(R.id.text); tv.setBackground(redDrawable);

Example XML code

<TextView android:layout_width="fill_parent"

android:layout_height="wrap_content" android:textAlign="center"

android:background="@drawable/solid_red"/>

0 0

Post a comment