More Resource Types

This page defines more types of resources you can externalize, including:

Bool
XML resource that carries a boolean value.
Color
XML resource that carries a color value (a hexadecimal color).
Dimension
XML resource that carries a dimension value (with a unit of measure).
ID
XML resource that provides a unique identifier for application resources and components.
Integer
XML resource that carries an integer value.
Integer Array
XML resource that provides an array of integers.
Typed Array
XML resource that provides a  TypedArray (which you can use for an array of drawables).

Bool


A boolean value defined in XML.

Note: A bool is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine bool resources with other simple resources in the one XML file, under one <resources> element.

FILE LOCATION:
res/values/filename.xml
The filename is arbitrary. The  <bool> element's  name will be used as the resource ID.
RESOURCE REFERENCE:
In Java:  R.bool.bool_name
In XML:  @[package:]bool/bool_name
SYNTAX:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <bool
        name="bool_name"
        >[true | false]</bool>
</resources>
ELEMENTS:
<resources>
Required. This must be the root node.

No attributes.

<bool>
A boolean value:  true or  false.

attributes:

name
String. A name for the bool value. This will be used as the resource ID.
EXAMPLE:
XML file saved at  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>

This application code retrieves the boolean:

Resources res = getResources();
boolean screenIsSmall = res.getBoolean(R.bool.screen_small);

This layout XML uses the boolean for an attribute:

<ImageView
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:src="@drawable/logo"
    android:adjustViewBounds="@bool/adjust_view_bounds" />

Color


A color value defined in XML. The color is specified with an RGB value and alpha channel. You can use a color resource any place that accepts a hexadecimal color value. You can also use a color resource when a drawable resource is expected in XML (for example, android:drawable="@color/green").

The value always begins with a pound (#) character and then followed by the Alpha-Red-Green-Blue information in one of the following formats:

  • #RGB
  • #ARGB
  • #RRGGBB
  • #AARRGGBB

Note: A color is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine color resources with other simple resources in the one XML file, under one <resources> element.

FILE LOCATION:
res/values/colors.xml
The filename is arbitrary. The  <color> element's  name will be used as the resource ID.
RESOURCE REFERENCE:
In Java:  R.color.color_name
In XML:  @[package:]color/color_name
SYNTAX:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color
        name="color_name"
        >hex_color</color>
</resources>
ELEMENTS:
<resources>
Required. This must be the root node.

No attributes.

<color>
A color expressed in hexadecimal, as described above.

attributes:

name
String. A name for the color. This will be used as the resource ID.
EXAMPLE:
XML file saved at  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>

This application code retrieves the color resource:

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

This layout XML applies the color to an attribute:

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textColor="@color/translucent_red"
    android:text="Hello"/>

Dimension


A dimension value defined in XML. A dimension is specified with a number followed by a unit of measure. For example: 10px, 2in, 5sp. The following units of measure are supported by Android:

dp
Density-independent Pixels - An abstract unit that is based on the physical density of the screen. These units are relative to a 160 dpi (dots per inch) screen, on which 1dp is roughly equal to 1px. When running on a higher density screen, the number of pixels used to draw 1dp is scaled up by a factor appropriate for the screen's dpi. Likewise, when on a lower density screen, the number of pixels used for 1dp is scaled down. The ratio of dp-to-pixel will change with the screen density, but not necessarily in direct proportion. Using dp units (instead of px units) is a simple solution to making the view dimensions in your layout resize properly for different screen densities. In other words, it provides consistency for the real-world sizes of your UI elements across different devices.
sp
Scale-independent Pixels - This is like the dp unit, but it is also scaled by the user's font size preference. It is recommend you use this unit when specifying font sizes, so they will be adjusted for both the screen density and the user's preference.
pt
Points - 1/72 of an inch based on the physical size of the screen.
px
Pixels - Corresponds to actual pixels on the screen. This unit of measure is not recommended because the actual representation can vary across devices; each devices may have a different number of pixels per inch and may have more or fewer total pixels available on the screen.
mm
Millimeters - Based on the physical size of the screen.
in
Inches - Based on the physical size of the screen.

Note: A dimension is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine dimension resources with other simple resources in the one XML file, under one <resources> element.

FILE LOCATION:
res/values/filename.xml
The filename is arbitrary. The  <dimen> element's  name will be used as the resource ID.
RESOURCE REFERENCE:
In Java:  R.dimen.dimension_name
In XML:  @[package:]dimen/dimension_name
SYNTAX:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen
        name="dimension_name"
        >dimension</dimen>
</resources>
ELEMENTS:
<resources>
Required. This must be the root node.

No attributes.

<dimen>
A dimension, represented by a float, followed by a unit of measurement (dp, sp, pt, px, mm, in), as described above.

attributes:

name
String. A name for the dimension. This will be used as the resource ID.
EXAMPLE:
XML file saved at  res/values/dimens.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="textview_height">25dp</dimen>
    <dimen name="textview_width">150dp</dimen>
    <dimen name="ball_radius">30dp</dimen>
    <dimen name="font_size">16sp</dimen>
</resources>

This application code retrieves a dimension:

Resources res = getResources();
float fontSize = res.getDimension(R.dimen.font_size);

This layout XML applies dimensions to attributes:

<TextView
    android:layout_height="@dimen/textview_height"
    android:layout_width="@dimen/textview_width"
    android:textSize="@dimen/font_size"/>

ID


A unique resource ID defined in XML. Using the name you provide in the <item> element, the Android developer tools create a unique integer in your project's R.java class, which you can use as an identifier for an application resources (for example, a View in your UI layout) or a unique integer for use in your application code (for example, as an ID for a dialog or a result code).

Note: An ID is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine ID resources with other simple resources in the one XML file, under one <resources> element. Also, remember that an ID resources does not reference an actual resource item; it is simply a unique ID that you can attach to other resources or use as a unique integer in your application.

FILE LOCATION:
res/values/filename.xml
The filename is arbitrary.
RESOURCE REFERENCE:
In Java:  R.id.name
In XML:  @[package:]id/name
SYNTAX:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item
        type="id"
        name="id_name" />
</resources>
ELEMENTS:
<resources>
Required. This must be the root node.

No attributes.

<item>
Defines a unique ID. Takes no value, only attributes.

attributes:

type
Must be "id".
name
String. A unique name for the ID.
EXAMPLE:

XML file saved at res/values/ids.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="id" name="button_ok" />
    <item type="id" name="dialog_exit" />
</resources>

Then, this layout snippet uses the "button_ok" ID for a Button widget:

<Button android:id="@id/button_ok"
    style="@style/button_style" />

Notice that the android:id value does not include the plus sign in the ID reference, because the ID already exists, as defined in the ids.xml example above. (When you specify an ID to an XML resource using the plus sign—in the format android:id="@+id/name"—it means that the "name" ID does not exist and should be created.)

As another example, the following code snippet uses the "dialog_exit" ID as a unique identifier for a dialog:

showDialog(R.id.dialog_exit);

In the same application, the "dialog_exit" ID is compared when creating a dialog:

protected Dialog onCreateDialog(int)(int id) {
    Dialog dialog;
    switch(id) {
    case R.id.dialog_exit:
        ...
        break;
    default:
        dialog = null;
    }
    return dialog;
}

Integer


An integer defined in XML.

Note: An integer is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine integer resources with other simple resources in the one XML file, under one <resources> element.

FILE LOCATION:
res/values/filename.xml
The filename is arbitrary. The  <integer> element's  name will be used as the resource ID.
RESOURCE REFERENCE:
In Java:  R.integer.integer_name
In XML:  @[package:]integer/integer_name
SYNTAX:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer
        name="integer_name"
        >integer</integer>
</resources>
ELEMENTS:
<resources>
Required. This must be the root node.

No attributes.

<integer>
An integer.

attributes:

name
String. A name for the integer. This will be used as the resource ID.
EXAMPLE:

XML file saved at res/values/integers.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer name="max_speed">75</integer>
    <integer name="min_speed">5</integer>
</resources>

This application code retrieves an integer:

Resources res = getResources();
int maxSpeed = res.getInteger(R.integer.max_speed);

Integer Array


An array of integers defined in XML.

Note: An integer array is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine integer array resources with other simple resources in the one XML file, under one <resources> element.

FILE LOCATION:
res/values/filename.xml
The filename is arbitrary. The  <integer-array> element's  name will be used as the resource ID.
COMPILED RESOURCE DATATYPE:
Resource pointer to an array of integers.
RESOURCE REFERENCE:
In Java:  R.array.integer_array_name
In XML:  @[package:]array.integer_array_name
SYNTAX:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer-array
        name="integer_array_name">
        <item
            >integer</item>
    </integer-array>
</resources>
ELEMENTS:
<resources>
Required. This must be the root node.

No attributes.

<integer-array>
Defines an array of integers. Contains one or more child  <item> elements.

attributes:

android:name
String. A name for the array. This name will be used as the resource ID to reference the array.
<item>
An integer. The value can be a reference to another integer resource. Must be a child of a <integer-array> element.

No attributes.

EXAMPLE:
XML file saved at  res/values/integers.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer-array name="bits">
        <item>4</item>
        <item>8</item>
        <item>16</item>
        <item>32</item>
    </integer-array>
</resources>

This application code retrieves the integer array:

Resources res = getResources();
int[] bits = res.getIntArray(R.array.bits);

Typed Array


TypedArray defined in XML. You can use this to create an array of other resources, such as drawables. Note that the array is not required to be homogeneous, so you can create an array of mixed resource types, but you must be aware of what and where the data types are in the array so that you can properly obtain each item with the TypedArray's get...() methods.

Note: A typed array is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine typed array resources with other simple resources in the one XML file, under one <resources> element.

FILE LOCATION:
res/values/filename.xml
The filename is arbitrary. The  <array> element's  name will be used as the resource ID.
COMPILED RESOURCE DATATYPE:
Resource pointer to a  TypedArray.
RESOURCE REFERENCE:
In Java:  R.array.array_name
In XML:  @[package:]array.array_name
SYNTAX:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array
        name="integer_array_name">
        <item>resource</item>
    </array>
</resources>
ELEMENTS:
<resources>
Required. This must be the root node.

No attributes.

<array>
Defines an array. Contains one or more child  <item> elements.

attributes:

android:name
String. A name for the array. This name will be used as the resource ID to reference the array.
<item>
A generic resource. The value can be a reference to a resource or a simple data type. Must be a child of an  <array> element.

No attributes.

EXAMPLE:
XML file saved at  res/values/arrays.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="icons">
        <item>@drawable/home</item>
        <item>@drawable/settings</item>
        <item>@drawable/logout</item>
    </array>
    <array name="colors">
        <item>#FFFF0000</item>
        <item>#FF00FF00</item>
        <item>#FF0000FF</item>
    </array>
</resources>

This application code retrieves each array and then obtains the first entry in each array:

Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);

TypedArray colors = res.obtainTypedArray(R.array.colors);
int color = colors.getColor(0,0);

此页面定义更多类型你可以外部化,包括资源:

布尔
携带一个布尔值XML资源。
颜色
承载颜色值(十六进制的颜色)XML资源。
尺寸
携带一维值(用计量单位)的XML资源。
ID
XML资源提供用于应用程序资源和组件的唯一标识符。
整型
携带一个整数值XML资源。
整数数组
XML资源提供整数数组。
类型数组
XML资源,提供一个 TypedArray(你可以使用可绘数组)。

布尔


在XML中定义一个布尔值。

注:一个布尔值是使用所提供的数值引用一个简单的资源名称属性(而不是XML文件的名称)。因此,您可以布尔资源与其他资源的简单一个XML文件中结合起来,在一个<资源>元素。

文件位置:
RES /价值/ 文件名 ​​的.xml 的文件名 ​​是任意的。在 <布尔>元素的 名称将用作资源ID。
资源引用:
在Java: 。R.bool bool_name 在XML: @ [ :]布尔/ bool_name
句法:
<?XML版本= “1.0” 编码= “UTF-8” ?> 
< 资源 > 
    < 布尔
        = bool_name
        > [真| 假] </布尔> 
</资源>
内容:
<资源>
必选项。这必须是根节点。

无属性。

<布尔>
一个布尔值: 假的

属性:

名称
字符串。一种用于布尔值名称。这将被用作资源ID。
例:
在保存XML文件 RES /值小/ bools.xml
<?xml的version = "1.0" encoding = "utf-8" ?> 
<resources> 
    <bool  name = "screen_small" > true </bool> 
    <bool  name = "adjust_view_bounds" > true </bool> 
</resources>

此应用程序代码检索布尔:

资源RES =  getResources () ; 
布尔screenIsSmall = 资源getBooleanř 布尔screen_small );

该布局XML使用一个属性布尔:

<ImageView 
    android:layout_height = "fill_parent" 
    android:layout_width = "fill_parent" 
    android:src = "@drawable/logo" 
    android:adjustViewBounds = "@bool/adjust_view_bounds"  />

颜色


在XML定义的颜色值。颜色与RGB值和Alpha通道指定。您可以使用颜色资源,它接受一个十六进制颜色值的任何地方。您还可以使用颜色资源时,绘制资源在XML预期(例如,机器人:可绘制=“@色/绿”)。

该值总是以磅(#)字符开头,然后接着在下面的格式之一阿尔法 - 红 - 绿 - 蓝信息:

  • RGB
  • ARGB
  • RRGGBB
  • AARRGGBB

注意:颜色是使用在所提供的值引用的一个简单的资源名称属性(未XML文件的名称)。因此,您可以在一个XML文件中与其他简单的整合资源颜色资源,在一个<资源>元素。

文件位置:
RES /价值/ colors.xml
文件名 ​​是任意的。在 <色彩>元素的 名称将用作资源ID。
资源引用:
在Java: 。R.color COLOR_NAME
在XML: @ [ :]彩色/ COLOR_NAME
句法:
<?XML版本= “1.0” 编码= “UTF-8” ?> 
< 资源 > 
    < 
        = COLOR_NAME
        > hex_color </颜色> 
</资源>
内容:
<资源>
必选项。这必须是根节点。

无属性。

<色>
一种彩色十六进制表示,如上所述。

属性:

名称
字符串。一种颜色名称。这将被用作资源ID。
例:
在保存XML文件 RES /价值/ colors.xml
<?xml的version = "1.0" encoding = "utf-8" ?> 
<resources> 
   <color  name = "opaque_red" > #f00 </color> 
   <color  name = "translucent_red" > #80ff0000 </color> 
</resources>

此应用程序代码检索颜色资源:

资源RES =  getResources () ; 
INT 颜色= 资源的getColorř opaque_red );

这种布局XML应用颜色的属性:

<TextView 
    android:layout_width = "fill_parent" 
    android:layout_height = "wrap_content" 
    android:textColor = "@color/translucent_red" 
    android:text = "Hello" />

尺寸


在XML定义的标注值。维度与一个数字,后跟度量单位指定。例如:10px的,2英寸,5SP。下列度量单位Android支持:

DP
密度独立像素 - 即是基于屏幕的物理密度的抽象单元。这些单位是相对于160 DPI(每英寸点数)的屏幕,其上1DP大致相等为1px。当一个较高密度屏幕上运行,用于绘制1DP像素的数量是由一个因子相应于屏幕的dpi的按比例放大。同样,较低的密度在屏幕上时,用于1DP像素的数量按比例缩小。DP到像素的比例将与屏幕密度改变,但不一定成正比。使用dp单位(而不是PX单位)是一个简单的解决方案,以使在布局视图尺寸适当调整为不同的屏幕密度。换句话说,它提供了在不同的装置上的UI元素的真实世界的尺寸的一致性。
SP
比例无关像素 - 这是像DP单元,但它也由用户的字体大小偏好缩放。它建议在指定的字体大小时使用此单元中,所以它们将被调整为两个屏幕密度和用户的偏好。
PT
点 - 根据屏幕的物理尺寸1/72英寸。
PX
像素 - 对应于屏幕上的实际像素。不建议这些单位因为实际的表示可以在不同设备有所不同; 每个设备可以具有不同数量的每英寸的像素,并且可以具有在画面上可用的更多或更少的总像素。
毫米
毫米 - 基于屏幕的物理尺寸。
英寸 - 基于屏幕的物理尺寸。

注意:维度是使用在所提供的值引用的一个简单的资源名称属性(未XML文件的名称)。因此,您可以维度的资源与其他资源的简单一个XML文件中结合起来,在一个<资源>元素。

文件位置:
RES /价值/ 文件名 ​​的.xml
的文件名 ​​是任意的。在 <扪>元素的 名称将用作资源ID。
资源引用:
在Java: 。R.dimen DIMENSION_NAME
在XML: @ [ :]扪/ DIMENSION_NAME
句法:
<?XML版本= “1.0” 编码= “UTF-8” ?> 
< 资源 > 
    < 
        = DIMENSION_NAME
        > 尺寸 </扪> 
</资源>
内容:
<资源>
必选项。这必须是根节点。

无属性。

<扪>
维,通过浮法表示,随后的测量单位(DP,SP,点,像素毫米,),如上所述。

属性:

名称
字符串。一种维度名称。这将被用作资源ID。
例:
在保存XML文件 RES /价值/ dimens.xml
<?xml的version = "1.0" encoding = "utf-8" ?> 
<resources> 
    <dimen  name = "textview_height" > 25dp </dimen> 
    <dimen  name = "textview_width" > 150dp </dimen> 
    <dimen  name = "ball_radius" > 30dp </dimen> 
    <dimen  name = "font_size" > 16sp </dimen> 
</resources>

此应用程序代码检索维度:

资源RES =  getResources () ; 
浮动fontSize的= 资源getDimensionř FONT_SIZE );

这种布局XML适用尺寸属性:

<TextView 
    android:layout_height = "@dimen/textview_height" 
    android:layout_width = "@dimen/textview_width" 
    android:textSize = "@dimen/font_size" />

ID


在XML中定义的唯一资源ID。使用您在提供姓名的<item> 元素,Android开发工具创建项目中的唯一的整 ​​数R.java类,它可以作为标识符使用一个应用程序的资源(例如,查看你的UI布局)或用于应用程序代码的使用(例如,如针对对话或结果的代码的ID)的唯一整数。

注意:一个ID是使 ​​用在提供的值引用的一个简单的资源名称属性(未XML文件的名称)。因此,您可以标识资源与其他资源的简单一个XML文件中结合起来,在一个<资源>元素。此外,请记住一个ID资源不引用实际的资源项目; 它只是一个唯一的ID,你可以连接到其他资源或应用程序中的一个唯一的整 ​​数使用。

文件位置:
RES /价值/ filename.xml中
的文件名 ​​是任意的。
资源引用:
在Java: R.id. 
在XML: @ [ :] ID / 名称
句法:
<?XML版本= “1.0” 编码= “UTF-8” ?> 
< 资源 > 
    < 项目
        类型= “ID” 
        的名字= ID_NAME /> 
</资源>
内容:
<资源>
必选项。这必须是根节点。

无属性。

<项目>
定义一个唯一的ID。没有值,只有属性。

属性:

类型
必须是“ID”。
名称
字符串。的唯一名称标识。
例:

在保存XML文件RES /价值/ ids.xml

<?xml的version = "1.0" encoding = "utf-8" ?> 
<resources> 
    <item  type = "id"  name = "button_ok"  /> 
    <item  type = "id"  name = "dialog_exit"  /> 
</resources>

然后,这种布局片断使用“button_ok”ID为Button控件:

<按钮 机器人:ID = @ ID / button_ok
    风格= @ 风格/ button_style  />

请注意,Android的:ID值不包括在ID参考的加号,因为该ID已经存在,如在规定ids.xml上面的例子。(当使用加号,格式指定ID为一个XML资源机器人:ID =“@ + ID /名称”。 -它意味着“名”ID不存在,应创建)

作为另一个例子,下面的代码片断使用“dialog_exit”ID作为针对对话的唯一标识符:

的ShowDialogř ID dialog_exit);

在相同的应用程序,创建一个对话的时候,“dialog_exit”ID进行比较:

protected  Dialog  onCreateDialog ( int ) ( int id )  { 
    Dialog dialog ; 
    switch ( id )  { 
    case  R . id . dialog_exit : 
        ... 
        break ; 
    default : 
        dialog =  null ; 
    } 
    return dialog ; 
}

整型


在XML中定义的整数。

注意:一个整数是使用在所提供的值引用的一个简单的资源名称属性(未XML文件的名称)。因此,您可以在整数的资源与其他资源的简单一个XML文件中结合起来,在一个<资源>元素。

文件位置:
RES /价值/ filename.xml中
的文件名 ​​是任意的。在 <整数>元素的 名称将用作资源ID。
资源引用:
在Java: 。R.integer integer_name
在XML: @ [ :]整数/ integer_name
句法:
<?XML版本= “1.0” 编码= “UTF-8” ?> 
< 资源 > 
    < 整数
        = integer_name
        > 整数 </整数> 
</资源>
内容:
<资源>
必选项。这必须是根节点。

无属性。

<整数>
一个整数。

属性:

名称
字符串。一种整数名称。这将被用作资源ID。
例:

在保存XML文件RES /价值/ integers.xml

<?xml的version = "1.0" encoding = "utf-8" ?> 
<resources> 
    <integer  name = "max_speed" > 75 </integer> 
    <integer  name = "min_speed" > 5 </integer> 
</resources>

此应用程序代码检索一个整数:

资源RES =  getResources () ; 
诠释MAXSPEED = 资源getIntegerř 整型MAX_SPEED );

整数数组


在XML定义整型数组。

注意:一个整数数组是通过在所提供的值引用的一个简单的资源名称属性(未XML文件的名称)。因此,您可以在一个XML文件中与其他简单的资源整合整数数组的资源,在一个<资源>元素。

文件位置:
RES /价值/ 文件名 ​​的.xml
的文件名 ​​是任意的。所述 <整数阵列>元素的 名字将被用作资源ID。
编译的资源数据类型:
资源指向整数数组。
资源引用:
在Java: 。R.array integer_array_name
在XML: @ [ :]数组integer_array_name
句法:
<?xml的version = "1.0" encoding = "utf-8" ?> 
< resources > 
    < integer-array
         name = " integer_array_name " > 
        < item
             > integer </item> 
    </integer-array> 
</resources>
内容:
<资源>
必选项。这必须是根节点。

无属性。

<整数数组>
定义一个整数数组。包含一个或多个子女 的<item>元素。

属性:

安卓:名称
字符串。一种数组名。这个名称将被作为资源ID,以引用的阵列。
<项目>
一个整数。该值可以是另一个整数资源的参考。必须是一个孩子 <整数数组>元素。

无属性。

例:
在保存XML文件 RES /价值/ integers.xml
<?xml的version = "1.0" encoding = "utf-8" ?> 
<resources> 
    <integer-array  name = "bits" > 
        <item> 4 </item> 
        <item> 8 </item> 
        <item> 16 </item> 
        <item> 32 </item> 
    </integer-array> 
</resources>

此应用程序代码检索整数数组:

资源RES =  getResources () ; 
诠释[] = 资源getIntArrayř 阵列);

类型数组


一个TypedArray在XML中定义。您可以使用它来 ​​创建其他资源,如可绘制的数组。请注意,是均匀阵列不是必需的,这样你就可以创建混合的资源类型的数组,但你必须知道什么,其中的数据类型数组中,这样就可以正确地获得与每个项目TypedArray “小号弄...()方法。

注意:类型化阵列是使用在提供的值引用的一个简单的资源名称属性(未XML文件的名称)。因此,您可以在一个XML文件中与其他简单的资源整合类型数组的资源,在一个<资源>元素。

文件位置:
RES /价值/ 文件名 ​​的.xml
的文件名 ​​是任意的。在 <数组>元素的 名称将用作资源ID。
编译的资源数据类型:
资源指针 TypedArray
资源引用:
在Java: 。R.array ARRAY_NAME
在XML: @ [ :]数组ARRAY_NAME
句法:
<?xml的version = "1.0" encoding = "utf-8" ?> 
< resources > 
    < array
         name = " integer_array_name " > 
        < item > resource </item> 
    </array> 
</resources>
内容:
<资源>
必选项。这必须是根节点。

无属性。

<数组>
定义的阵列。包含一个或多个子女 的<item>元素。

属性:

安卓:名称
字符串。一种数组名。这个名称将被作为资源ID,以引用的阵列。
<项目>
通用资源。该值可以是一个资源或简单数据类型的引用。必须是一个孩子 <数组>元素。

无属性。

例:
在保存XML文件 RES /价值/ arrays.xml
<?xml的version = "1.0" encoding = "utf-8" ?> 
<resources> 
    <array  name = "icons" > 
        <item> @drawable/home </item> 
        <item> @drawable/settings </item> 
        <item> @drawable/logout </item> 
    </array> 
    <array  name = "colors" > 
        <item> #FFFF0000 </item> 
        <item> #FF00FF00 </item> 
        <item> #FF0000FF </item> 
    </array> 
</resources>

此应用程序代码检索每个数组,然后获得每个数组中的第一项:

Resources res =  getResources () ; 
TypedArray icons = res . obtainTypedArray ( R . array . icons ); 
Drawable drawable = icons . getDrawable ( 0 ); 

TypedArray colors = res . obtainTypedArray ( R . array . colors ); 
int color = colors . getColor ( 0 , 0 );

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值