Most visited

Recently visited

Added in API level 1
Deprecated since API level 24

MockResources

public class MockResources
extends Resources

java.lang.Object
   ↳ android.content.res.Resources
     ↳ android.test.mock.MockResources


此类在API级别24中已被弃用。
使用模拟框架,如Mockito 应使用Android Testing Support Library编写新测试。

模拟Resources类。 所有的方法都是非功能性的,并抛出UnsupportedOperationException 重写它以提供您需要的操作。

Summary

Public constructors

MockResources()

Public methods

XmlResourceParser getAnimation(int id)

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

int getColor(int id)

此方法已弃用。 改为使用getColor(int, Theme)

ColorStateList getColorStateList(int id)

此方法已弃用。 改为使用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)

此方法已弃用。 改为使用getDrawable(int, Theme)

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, Object... formatArgs)

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

String getQuantityString(int id, int quantity)

返回给定数量的给定资源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)

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

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

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

String[] getStringArray(int id)

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

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关联的原始数据。

XmlResourceParser getXml(int id)

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

TypedArray obtainAttributes(AttributeSet set, int[] attrs)

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

TypedArray obtainTypedArray(int id)

返回一个异构值的数组。

InputStream openRawResource(int id)

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

AssetFileDescriptor openRawResourceFd(int id)

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

void updateConfiguration(Configuration config, DisplayMetrics metrics)

存储最新更新的配置。

Inherited methods

From class android.content.res.Resources
From class java.lang.Object

Public constructors

MockResources

Added in API level 1
MockResources ()

Public methods

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

getColor

Added in API level 1
int getColor (int id)

此方法已弃用。
改为使用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

getColorStateList

Added in API level 1
ColorStateList getColorStateList (int id)

此方法已弃用。
改为使用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

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

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

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

getDisplayMetrics

Added in API level 1
DisplayMetrics getDisplayMetrics ()

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

Returns
DisplayMetrics The resource's current display metrics.

getDrawable

Added in API level 1
Drawable getDrawable (int id)

此方法已弃用。
改为使用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

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

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

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

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

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

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

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

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

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

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

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

getString

Added in API level 1
String getString (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 String The string data associated with the resource, stripped of styled text information.
Throws
Resources.NotFoundException

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

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

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

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

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

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

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

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

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

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

updateConfiguration

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

存储最新更新的配置。

Parameters
config Configuration
metrics DisplayMetrics

Hooray!