Most visited

Recently visited

Added in API level 1

Resources

public class Resources
extends Object

java.lang.Object
   ↳ android.content.res.Resources
Known Direct Subclasses


访问应用程序资源的类。 它位于应用程序的资产管理器之上(可通过getAssets()访问),并提供用于从资产获取类型化数据的高级API。

Android资源系统会跟踪与应用程序关联的所有非代码资产。 你可以使用这个类来访问你的应用程序的资源。 通常你可以获取Resources与应用程序相关的实例getResources()

构建时,Android SDK工具将应用程序的资源编译到应用程序二进制文件中。 要使用资源,必须将它正确安装在源代码树(在项目的res/目录中)并构建您的应用程序。 作为构建过程的一部分,SDK工具为每个资源生成符号,您可以在应用程序代码中使用这些符号来访问资源。

使用应用程序资源可以轻松更新应用程序的各种特性,而无需修改代码,并且 - 通过提供多种备用资源 - 使您能够针对各种设备配置(例如针对不同语言和屏幕尺寸)优化您的应用程序, 。 这是开发兼容不同类型设备的Android应用程序的一个重要方面。

有关使用资源的更多信息,请参阅有关 Application Resources的文档。

Summary

Nested classes

class Resources.NotFoundException

当找不到请求的资源时,资源API会抛出此异常。

class Resources.Theme

该类保存特定主题的当前属性值。

Public constructors

Resources(AssetManager assets, DisplayMetrics metrics, Configuration config)

在AssetManager中现有的一组资产上创建一个新的资源对象。

Public methods

final void finishPreloading()

当zygote完成预加载资源时调用,以更改回正常的资源操作。

final void flushLayoutCache()

调用此方法从资源对象中删除所有缓存加载的布局资源。

XmlResourceParser getAnimation(int id)

返回一个XmlResourceParser,通过它可以读取给定资源ID的动画描述。

final AssetManager getAssets()

为这些资源检索底层AssetManager存储。

boolean getBoolean(int id)

返回与特定资源ID关联的布尔值。

int getColor(int id)

此方法在API级别23中已弃用。请改为使用getColor(int, Theme)

int getColor(int id, Resources.Theme theme)

返回与特定资源ID关联的主题颜色整数。

ColorStateList getColorStateList(int id, Resources.Theme theme)

返回与特定资源ID关联的主题颜色状态列表。

ColorStateList getColorStateList(int id)

此方法在API级别23中已弃用。请改为使用getColorStateList(int, Theme)

Configuration getConfiguration()

返回此资源对象的当前配置。

float getDimension(int id)

检索特定资源ID的维度。

int getDimensionPixelOffset(int id)

检索特定资源ID的维度以用作原始像素中的偏移量。

int getDimensionPixelSize(int id)

检索特定资源ID的尺寸以用作原始像素的尺寸。

DisplayMetrics getDisplayMetrics()

返回对这个资源对象有效的当前显示度量。

Drawable getDrawable(int id, Resources.Theme theme)

返回与特定资源ID关联的可绘制对象,并为指定的主题创建样式。

Drawable getDrawable(int id)

此方法在API级别22中已弃用。请改为使用getDrawable(int, Theme)

Drawable getDrawableForDensity(int id, int density)

此方法在API级别22中已弃用。请改为使用getDrawableForDensity(int, int, Theme)

Drawable getDrawableForDensity(int id, int density, Resources.Theme theme)

返回与DPI中给定屏幕密度的特定资源ID关联的可绘制对象,并为指定的主题创建样式。

float getFraction(int id, int base, int pbase)

检索特定资源ID的小数单位。

int getIdentifier(String name, String defType, String defPackage)

返回给定资源名称的资源标识符。

int[] getIntArray(int id)

返回与特定资源ID关联的int数组。

int getInteger(int id)

返回与特定资源ID关联的整数。

XmlResourceParser getLayout(int id)

返回一个XmlResourceParser,通过它可以读取给定资源ID的视图布局描述。

Movie getMovie(int id)

返回与特定资源ID关联的电影对象。

String getQuantityString(int id, int quantity)

返回给定数量的给定资源ID的语法正确多元化所需的字符串。

String getQuantityString(int id, int quantity, Object... formatArgs)

使用给定的参数,格式化为给定数量的语法正确复数化给定资源ID所必需的字符串。

CharSequence getQuantityText(int id, int quantity)

返回给定数量的语法正确复数化给定资源ID所需的字符序列。

String getResourceEntryName(int resid)

返回给定资源标识符的条目名称。

String getResourceName(int resid)

返回给定资源标识符的全名。

String getResourcePackageName(int resid)

返回给定资源标识符的包名称。

String getResourceTypeName(int resid)

返回给定资源标识符的类型名称。

String getString(int id, Object... formatArgs)

返回与特定资源ID关联的字符串值,替换Formatterformat(String, Object...)定义的格式参数。 它将被删除任何样式的文本信息。

String getString(int id)

返回与特定资源ID关联的字符串值。 它将被删除任何样式的文本信息。

String[] getStringArray(int id)

返回与特定资源ID关联的字符串数组。

static Resources getSystem()

返回全局共享资源对象,该对象仅提供对系统资源(无应用程序资源)的访问权限,并且未为当前屏幕配置(不能使用维度单位,不根据方向等进行更改)。

CharSequence getText(int id, CharSequence def)

返回与特定资源ID关联的字符串值。

CharSequence getText(int id)

返回与特定资源ID关联的字符串值。 如果这是一个纯字符串,则返回的对象将是一个字符串; 如果它是风格的,它将是一些其他类型的CharSequence。

CharSequence[] getTextArray(int id)

返回与特定资源ID关联的样式文本数组。

void getValue(String name, TypedValue outValue, boolean resolveRefs)

返回与特定资源ID关联的原始数据。

void getValue(int id, TypedValue outValue, boolean resolveRefs)

返回与特定资源ID关联的原始数据。

void getValueForDensity(int id, int density, TypedValue outValue, boolean resolveRefs)

获取与具有相关密度的资源相关的原始值。

XmlResourceParser getXml(int id)

返回一个XmlResourceParser,通过它可以读取给定资源ID的通用XML资源。

final Resources.Theme newTheme()

为这组资源生成一个新的主题对象。

TypedArray obtainAttributes(AttributeSet set, int[] attrs)

从AttributeSet中检索一组基本属性值,而不是使用主题和/或样式资源对它们进行样式设置。

TypedArray obtainTypedArray(int id)

返回一个异构值的数组。

InputStream openRawResource(int id, TypedValue value)

打开数据流以读取原始资源。

InputStream openRawResource(int id)

打开数据流以读取原始资源。

AssetFileDescriptor openRawResourceFd(int id)

打开一个文件描述符来读取原始资源。

void parseBundleExtra(String tagName, AttributeSet attrs, Bundle outBundle)

从包含该数据的XML标记中解析名称/值对。

void parseBundleExtras(XmlResourceParser parser, Bundle outBundle)

从XML文件中解析一系列 <extra>标签。

void updateConfiguration(Configuration config, DisplayMetrics metrics)

存储最新更新的配置。

Inherited methods

From class java.lang.Object

Public constructors

Resources

Added in API level 1
Resources (AssetManager assets, 
                DisplayMetrics metrics, 
                Configuration config)

在AssetManager中现有的一组资产上创建一个新的资源对象。

Parameters
assets AssetManager: Previously created AssetManager.
metrics DisplayMetrics: Current display metrics to consider when selecting/computing resource values.
config Configuration: Desired device configuration to consider when selecting/computing resource values (optional).

Public methods

finishPreloading

Added in API level 1
void finishPreloading ()

当zygote完成预加载资源时调用,以更改回正常的资源操作。

flushLayoutCache

Added in API level 1
void flushLayoutCache ()

调用此方法从资源对象中删除所有缓存加载的布局资源。 仅用于性能测试工具。

getAnimation

Added in API level 1
XmlResourceParser getAnimation (int id)

返回一个XmlResourceParser,通过它可以读取给定资源ID的动画描述。 此解析器的功能有限 - 特别是,您无法更改其输入,并且只有高级别事件可用。

这个函数实际上是一个用动画资源调用 getXml(int)的简单包装。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
XmlResourceParser A new parser object through which you can read the XML data.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

也可以看看:

getAssets

Added in API level 1
AssetManager getAssets ()

为这些资源检索底层AssetManager存储。

Returns
AssetManager

getBoolean

Added in API level 3
boolean getBoolean (int id)

返回与特定资源ID关联的布尔值。 这可以与任何整型资源值一起使用,并且如果它不为零则返回true。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
boolean Returns the boolean value contained in the resource.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getColor

Added in API level 1
int getColor (int id)

此方法在API级别23中已弃用。
改为使用getColor(int, Theme)

返回与特定资源ID关联的颜色整数。 如果资源包含复杂的ColorStateList ,则返回该集合中的默认颜色。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
int A single color value in the form 0xAARRGGBB.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getColor

Added in API level 23
int getColor (int id, 
                Resources.Theme theme)

返回与特定资源ID关联的主题颜色整数。 如果资源包含复杂的ColorStateList ,则返回该集合中的默认颜色。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
theme Resources.Theme: The theme used to style the color attributes, may be null.
Returns
int A single color value in the form 0xAARRGGBB.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getColorStateList

Added in API level 23
ColorStateList getColorStateList (int id, 
                Resources.Theme theme)

返回与特定资源ID关联的主题颜色状态列表。 该资源可能包含一个原始颜色值或包含多种可能颜色的复杂ColorStateList

Parameters
id int: The desired resource identifier of a ColorStateList, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
theme Resources.Theme: The theme used to style the color attributes, may be null.
Returns
ColorStateList A themed ColorStateList object containing either a single solid color or multiple colors that can be selected based on a state.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getColorStateList

Added in API level 1
ColorStateList getColorStateList (int id)

此方法在API级别23中已弃用。
改为使用getColorStateList(int, Theme)

返回与特定资源ID关联的颜色状态列表。 该资源可能包含单个原始颜色值或包含多种可能颜色的复杂ColorStateList

Parameters
id int: The desired resource identifier of a ColorStateList, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
ColorStateList A ColorStateList object containing either a single solid color or multiple colors that can be selected based on a state.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getConfiguration

Added in API level 1
Configuration getConfiguration ()

返回此资源对象的当前配置。 返回的对象应被视为只读。

Returns
Configuration The resource's current configuration.

getDimension

Added in API level 1
float getDimension (int id)

检索特定资源ID的维度。 单位转换基于与资源相关的当前DisplayMetrics

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
float Resource dimension value multiplied by the appropriate metric.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

也可以看看:

getDimensionPixelOffset

Added in API level 1
int getDimensionPixelOffset (int id)

检索特定资源ID的维度以用作原始像素中的偏移量。 这与getDimension(int)相同,只是返回的值将转换为整数像素。 偏移转换涉及将基本值简单地截断为整数。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
int Resource dimension value multiplied by the appropriate metric and truncated to integer pixels.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

也可以看看:

getDimensionPixelSize

Added in API level 1
int getDimensionPixelSize (int id)

检索特定资源ID的尺寸以用作原始像素的尺寸。 这与getDimension(int)相同,只是返回的值被转换为整数像素以用作大小。 大小转换包括舍入基值,并确保非零基值至少有一个像素大小。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
int Resource dimension value multiplied by the appropriate metric and truncated to integer pixels.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

也可以看看:

getDisplayMetrics

Added in API level 1
DisplayMetrics getDisplayMetrics ()

返回对这个资源对象有效的当前显示度量。 返回的对象应被视为只读。

Returns
DisplayMetrics The resource's current display metrics.

getDrawable

Added in API level 21
Drawable getDrawable (int id, 
                Resources.Theme theme)

返回与特定资源ID关联的可绘制对象,并为指定的主题创建样式。 根据底层资源返回各种类型的对象 - 例如,纯色,PNG图像,可缩放图像等。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
theme Resources.Theme: The theme used to style the drawable attributes, may be null.
Returns
Drawable Drawable An object that can be used to draw this resource.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getDrawable

Added in API level 1
Drawable getDrawable (int id)

此方法在API级别22中已弃用。
改为使用getDrawable(int, Theme)

返回与特定资源ID关联的可绘制对象。 根据底层资源返回各种类型的对象 - 例如,纯色,PNG图像,可缩放图像等。可绘制的API隐藏了这些实现细节。

注意:JELLY_BEAN之前,当通过此处传递的资源ID是另一个可绘制资源的别名时,此函数将无法正确检索最终配置密度。 这意味着如果别名资源的密度配置与实际资源不同,则返回的Drawable的密度将不正确,从而导致缩放不良。 要解决这个问题,可以改为手动解决通过使用别名参考getValue(int, TypedValue, boolean)并通过trueresolveRefs 由此产生的resourceId值可能会传递给此方法。

注:要获得主题绘图,请使用 Context.getDrawable(int)getDrawable(int, Theme)传递所需的主题。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
Drawable Drawable An object that can be used to draw this resource.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

也可以看看:

getDrawableForDensity

Added in API level 15
Drawable getDrawableForDensity (int id, 
                int density)

此方法在API级别22中已弃用。
改为使用getDrawableForDensity(int, int, Theme)

返回与DPI中给定屏幕密度的特定资源ID关联的可绘制对象。 这将设置drawable的密度为设备的密度乘以实际可提取密度与请求密度的比值。 这样可以根据需要将绘图放大到正确的大小。 根据底层资源返回各种类型的对象 - 例如,纯色,PNG图像,可缩放图像等。可绘制的API隐藏了这些实现细节。

注:要获得主题绘图,请使用 Context.getDrawable(int)getDrawableForDensity(int, int, Theme)传递所需的主题。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
density int: the desired screen density indicated by the resource as found in DisplayMetrics.
Returns
Drawable Drawable An object that can be used to draw this resource.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

也可以看看:

getDrawableForDensity

Added in API level 21
Drawable getDrawableForDensity (int id, 
                int density, 
                Resources.Theme theme)

返回与DPI中给定屏幕密度的特定资源ID关联的可绘制对象,并为指定的主题创建样式。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
density int: The desired screen density indicated by the resource as found in DisplayMetrics.
theme Resources.Theme: The theme used to style the drawable attributes, may be null.
Returns
Drawable Drawable An object that can be used to draw this resource.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getFraction

Added in API level 3
float getFraction (int id, 
                int base, 
                int pbase)

检索特定资源ID的小数单位。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
base int: The base value of this fraction. In other words, a standard fraction is multiplied by this value.
pbase int: The parent base value of this fraction. In other words, a parent fraction (nn%p) is multiplied by this value.
Returns
float Attribute fractional value multiplied by the appropriate base value.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getIdentifier

Added in API level 1
int getIdentifier (String name, 
                String defType, 
                String defPackage)

返回给定资源名称的资源标识符。 完全限定的资源名称形式为“package:type / entry”。 如果在这里指定defType和defPackage,前两个组件(包和类型)是可选的。

注意:不鼓励使用此功能。 通过标识符而不是按名称检索资源要高效得多。

Parameters
name String: The name of the desired resource.
defType String: Optional default resource type to find, if "type/" is not included in the name. Can be null to require an explicit type.
defPackage String: Optional default package to find, if "package:" is not included in the name. Can be null to require an explicit package.
Returns
int int The associated resource identifier. Returns 0 if no such resource was found. (0 is not a valid resource ID.)

getIntArray

Added in API level 1
int[] getIntArray (int id)

返回与特定资源ID关联的int数组。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
int[] The int array associated with the resource.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getInteger

Added in API level 1
int getInteger (int id)

返回与特定资源ID关联的整数。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
int Returns the integer value contained in the resource.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getLayout

Added in API level 1
XmlResourceParser getLayout (int id)

返回一个XmlResourceParser,通过它可以读取给定资源ID的视图布局描述。 此解析器的功能有限 - 特别是,您无法更改其输入,并且只有高级别事件可用。

这个函数实际上是一个用布局资源调用 getXml(int)的简单包装。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
XmlResourceParser A new parser object through which you can read the XML data.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

也可以看看:

getMovie

Added in API level 1
Movie getMovie (int id)

返回与特定资源ID关联的电影对象。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
Movie
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getQuantityString

Added in API level 1
String getQuantityString (int id, 
                int quantity)

返回给定数量的给定资源ID的语法正确多元化所需的字符串。 请注意,字符串仅根据语法必要性进行选择,并且这些规则在不同的语言中有所不同。 不要以为你知道给定数量的哪个字符串会被返回。 有关更多详细信息,请参阅String Resources

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
quantity int: The number used to get the correct string for the current language's plural rules.
Returns
String String The string data associated with the resource, stripped of styled text information.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getQuantityString

Added in API level 1
String getQuantityString (int id, 
                int quantity, 
                Object... formatArgs)

使用给定的参数,格式化为给定数量的语法正确复数化给定资源ID所必需的字符串。 请注意,字符串仅根据语法必要性进行选择,并且这些规则在不同的语言中有所不同。 不要以为你知道给定数量的哪个字符串会被返回。 详情请参阅String Resources

格式参数的替换就像使用Formatterformat(String, Object...) 结果字符串将被剥离任何样式的文本信息。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
quantity int: The number used to get the correct string for the current language's plural rules.
formatArgs Object: The format arguments that will be used for substitution.
Returns
String String The string data associated with the resource, stripped of styled text information.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getQuantityText

Added in API level 1
CharSequence getQuantityText (int id, 
                int quantity)

返回给定数量的语法正确复数化给定资源ID所需的字符序列。 请注意,字符序列仅根据语法必要性进行选择,并且这些规则在不同的语言中有所不同。 不要以为你知道给定数量的哪个字符串会被返回。 有关更多详细信息,请参阅String Resources

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
quantity int: The number used to get the correct string for the current language's plural rules.
Returns
CharSequence CharSequence The string data associated with the resource, plus possibly styled text information.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getResourceEntryName

Added in API level 1
String getResourceEntryName (int resid)

返回给定资源标识符的条目名称。

Parameters
resid int: The resource identifier whose entry name is to be retrieved.
Returns
String A string holding the entry name of the resource.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

也可以看看:

getResourceName

Added in API level 1
String getResourceName (int resid)

返回给定资源标识符的全名。 该名称是“package:type / entry”形式的单个字符串。

Parameters
resid int: The resource identifier whose name is to be retrieved.
Returns
String A string holding the name of the resource.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

也可以看看:

getResourcePackageName

Added in API level 1
String getResourcePackageName (int resid)

返回给定资源标识符的包名称。

Parameters
resid int: The resource identifier whose package name is to be retrieved.
Returns
String A string holding the package name of the resource.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

也可以看看:

getResourceTypeName

Added in API level 1
String getResourceTypeName (int resid)

返回给定资源标识符的类型名称。

Parameters
resid int: The resource identifier whose type name is to be retrieved.
Returns
String A string holding the type name of the resource.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

也可以看看:

getString

Added in API level 1
String getString (int id, 
                Object... formatArgs)

返回与特定资源ID关联的字符串值,替换Formatterformat(String, Object...)定义的格式参数。 它将被删除任何样式的文本信息。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
formatArgs Object: The format arguments that will be used for substitution.
Returns
String String The string data associated with the resource, stripped of styled text information.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getString

Added in API level 1
String getString (int id)

Return the string value associated with a particular resource ID. It will be stripped of any styled text information.

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
String String The string data associated with the resource, stripped of styled text information.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getStringArray

Added in API level 1
String[] getStringArray (int id)

返回与特定资源ID关联的字符串数组。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
String[] The string array associated with the resource.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getSystem

Added in API level 1
Resources getSystem ()

返回全局共享资源对象,该对象仅提供对系统资源(无应用程序资源)的访问权限,并且未为当前屏幕配置(不能使用维度单位,不根据方向等进行更改)。

Returns
Resources

getText

Added in API level 1
CharSequence getText (int id, 
                CharSequence def)

返回与特定资源ID关联的字符串值。 如果这是一个纯字符串,则返回的对象将是一个字符串; 如果它是风格的,它将是一些其他类型的CharSequence。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
def CharSequence: The default CharSequence to return.
Returns
CharSequence CharSequence The string data associated with the resource, plus possibly styled text information, or def if id is 0 or not found.

getText

Added in API level 1
CharSequence getText (int id)

返回与特定资源ID关联的字符串值。 如果这是一个纯字符串,则返回的对象将是一个字符串; 如果它是风格的,它将是一些其他类型的CharSequence。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
CharSequence CharSequence The string data associated with the resource, plus possibly styled text information.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getTextArray

Added in API level 1
CharSequence[] getTextArray (int id)

返回与特定资源ID关联的样式文本数组。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
CharSequence[] The styled text array associated with the resource.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getValue

Added in API level 1
void getValue (String name, 
                TypedValue outValue, 
                boolean resolveRefs)

返回与特定资源ID关联的原始数据。 有关名称如何映射到资源ID的信息,请参阅getIdentifier();有关如何检索字符串资源的信息,请参阅getString(int)。

注意:不鼓励使用此功能。 通过标识符而不是按名称检索资源要高效得多。

Parameters
name String: The name of the desired resource. This is passed to getIdentifier() with a default type of "string".
outValue TypedValue: Object in which to place the resource data.
resolveRefs boolean: If true, a resource that is a reference to another resource will be followed so that you receive the actual final resource data. If false, the TypedValue will be filled in with the reference itself.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getValue

Added in API level 1
void getValue (int id, 
                TypedValue outValue, 
                boolean resolveRefs)

返回与特定资源ID关联的原始数据。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
outValue TypedValue: Object in which to place the resource data.
resolveRefs boolean: If true, a resource that is a reference to another resource will be followed so that you receive the actual final resource data. If false, the TypedValue will be filled in with the reference itself.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

getValueForDensity

Added in API level 15
void getValueForDensity (int id, 
                int density, 
                TypedValue outValue, 
                boolean resolveRefs)

获取与具有相关密度的资源相关的原始值。

Parameters
id int: resource identifier
density int: density in DPI
outValue TypedValue
resolveRefs boolean: If true, a resource that is a reference to another resource will be followed so that you receive the actual final resource data. If false, the TypedValue will be filled in with the reference itself.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

也可以看看:

getXml

Added in API level 1
XmlResourceParser getXml (int id)

返回一个XmlResourceParser,通过它可以读取给定资源ID的通用XML资源。

这里返回的XmlPullParser实现有一些有限的功能。 特别是,你不能改变它的输入,并且只有高级分析事件可用(因为在构建时文档是为你预解析的,包括合并文本和剥离注释)。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
XmlResourceParser A new parser object through which you can read the XML data.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

也可以看看:

newTheme

Added in API level 1
Resources.Theme newTheme ()

为这组资源生成一个新的主题对象。 它最初开始是空的。

Returns
Resources.Theme Theme The newly created Theme container.

obtainAttributes

Added in API level 1
TypedArray obtainAttributes (AttributeSet set, 
                int[] attrs)

从AttributeSet中检索一组基本属性值,而不是使用主题和/或样式资源对它们进行样式设置。

Parameters
set AttributeSet: The current attribute values to retrieve.
attrs int: The specific attributes to be retrieved.
Returns
TypedArray Returns a TypedArray holding an array of the attribute values. Be sure to call TypedArray.recycle() when done with it.

也可以看看:

obtainTypedArray

Added in API level 1
TypedArray obtainTypedArray (int id)

返回一个异构值的数组。

Parameters
id int: The desired resource identifier, as generated by the aapt tool. This integer encodes the package, type, and resource entry. The value 0 is an invalid identifier.
Returns
TypedArray Returns a TypedArray holding an array of the array values. Be sure to call TypedArray.recycle() when done with it.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

openRawResource

Added in API level 3
InputStream openRawResource (int id, 
                TypedValue value)

打开数据流以读取原始资源。 这只能用于资源文件名称的资源 - 也就是说,它可以用来打开可绘制的,可靠的和原始的资源; 它将在字符串和颜色资源上失败。

Parameters
id int: The resource identifier to open, as generated by the appt tool.
value TypedValue: The TypedValue object to hold the resource information.
Returns
InputStream InputStream Access to the resource data.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

openRawResource

Added in API level 1
InputStream openRawResource (int id)

打开数据流以读取原始资源。 这只能用于其资源文件名称的资源 - 也就是说,它可以用于打开可绘制,可靠和原始资源; 它将在字符串和颜色资源上失败。

Parameters
id int: The resource identifier to open, as generated by the appt tool.
Returns
InputStream InputStream Access to the resource data.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

openRawResourceFd

Added in API level 1
AssetFileDescriptor openRawResourceFd (int id)

打开一个文件描述符来读取原始资源。 这只能用于其资源文件名称的资源 - 也就是说,它可以用于打开可绘制,可靠和原始资源; 它将在字符串和颜色资源上失败。

此功能仅适用于作为未压缩数据存储在软件包中的资源,其中通常包含诸如mp3文件和png图像等内容。

Parameters
id int: The resource identifier to open, as generated by the appt tool.
Returns
AssetFileDescriptor AssetFileDescriptor A new file descriptor you can use to read the resource. This includes the file descriptor itself, as well as the offset and length of data where the resource appears in the file. A null is returned if the file exists but is compressed.
Throws
Resources.NotFoundException Throws NotFoundException if the given ID does not exist.

parseBundleExtra

Added in API level 3
void parseBundleExtra (String tagName, 
                AttributeSet attrs, 
                Bundle outBundle)

从包含该数据的XML标记中解析名称/值对。 AttributeSet必须保存由Extra定义的数据。 支持以下值类型:

Parameters
tagName String: The name of the tag these attributes come from; this is only used for reporting error messages.
attrs AttributeSet: The attributes from which to retrieve the name/value pair.
outBundle Bundle: The Bundle in which to place the parsed value.
Throws
XmlPullParserException If the attributes are not valid.

parseBundleExtras

Added in API level 3
void parseBundleExtras (XmlResourceParser parser, 
                Bundle outBundle)

从XML文件中解析一系列<extra>标签。 当你在额外标签的父标签处时,你会调用它,并且一旦所有的子标签都被解析,它就会返回。 这将为每个遇到的额外标签调用parseBundleExtra(String, AttributeSet, Bundle)

Parameters
parser XmlResourceParser: The parser from which to retrieve the extras.
outBundle Bundle: A Bundle in which to place all parsed extras.
Throws
XmlPullParserException
IOException
XmlPullParserException

updateConfiguration

Added in API level 1
void updateConfiguration (Configuration config, 
                DisplayMetrics metrics)

存储最新更新的配置。

Parameters
config Configuration
metrics DisplayMetrics

Hooray!