Android Application resources-other resource types (more types) (1)

Source: Internet
Author: User

This document describes other resources in the Android system that can be defined outside the code, including:

Bool type resources:

An XML resource that defines a Boolean value.

Color resources:

XML resource that defines the color value (hexadecimal color value ).

Dimension type resources:

Defines the size of an XML Resource (with the unit of dimensions ).

ID type Resource:

Provide XML resources with unique identifiers for application resources and components.

Integer type resources:

The XML resource that defines the integer.

Integer array type resources:

Defines the XML resource of an integer array.

Typed array type resources:

Provides XML resources for typearray objects (such as drawble resource arrays ).

 

Bool Resources

Define a Boolean value in XML.

Note: A boolean value is a simple resource that is referenced by the value provided by the name attribute. For example, you can combine a Boolean resource with other simple resources under the <resources> element of an XML file.

File Location ):

Res/values/filename. xml

The file name is arbitrary. The <bool> element name attribute value is used as the resource ID.

Resource reference ):

In Java code: R. bool. bool_name;

In XML: @ [Package:] bool/bool_name.

Syntax (stntax ):

<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Bool
Name ="Bool_name"
> [True | false] </bool>
</Resources>

Element (elements ):

<Resources>

Required. It must be the root node. No attribute.

<Bool>

Defines a Boolean value: true or false

Attributes (attributes ):

Name
String value, which defines a name for the Boolean value. This name is used as the resource ID.

Example ):

The following XML is saved in RES/Values-small/bools. xml:

<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Bool
Name = "screen_small"> true </bool>
<Bool
Name = "adjust_view_bounds"> true </bool>
</Resources>

In the application code, use the following method to obtain the Boolean value:

Resources res =getResources();
Boolean screenissmall = res.getBoolean(R. bool. screen_small );

The method for using Boolean resources in layout XML:

<Imageview
Android: layout_height = "fill_parent"
Android: layout_width = "fill_parent"
Android: src = "@ drawable/logo"
Android: adjustviewbounds = "@ bool/adjust_view_bounds"/>

Color Resources

Define the color value in XML. The color is specified using the RGB value and alpha channel. Color resources can be used anywhere in the hexadecimal format. You can also use color resources to draw resources that are expected to be used in XML (for example, Android: drawable = "@ color/Green ").

The color value always starts with "#", followed by alpha-red-green-blue. The specific format is as follows:

# RGB

# Argb

# Rrggbb

# Aarrggbb

Note: A color resource is a simple resource that uses the value provided by the name attribute to reference the resource. For example, you can combine color resources and other simple Resources in the <resources> element of an XML file.

File Location ):

Res/values/colors. xml

The file name is arbitrary. The name attribute value of the <color> element is used as the resource ID.

Resource reference ):

In Java code: R. color. color_name

In XML: @ [Package:] color/color_name

Syntax (syntax ):

<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Color
Name ="Color_name"
>Hex_color</Color>
</Resources>

Element (elements ):

<Resources>

Required. It must be a root node and has no attribute.

<Color>

Use the hexadecimal format described above to represent a color value.

Attributes (attributes ):

Name


String value. Specify a name for the color resource. The value of this name attribute is used as the resource ID.

Example ):

The following XML is saved in RES/values/colors. xml:

<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<Color
Name = "opaque_red"> # f00 </color>
<Color
Name = "translucent_red"> #80ff0000 </color>
</Resources>

The following is how the application code obtains color resources:

Resources res =getResources();
Int color = res.getColor(R. color. opaque_red );

The following describes how to use color resources in an XML layout:

<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: textcolor = "@ color/translucent_red"
Android: text = "hello"/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.