`
jguangyou
  • 浏览: 375745 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

android value 资源

 
阅读更多

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.string_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.

<string-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 referenced 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


A 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);

 

分享到:
评论

相关推荐

    android适配工具,生成相应的Value

    "android适配工具"就是为了帮助开发者解决这个问题而设计的,它可以生成相应的Value资源文件,其中包含了针对不同屏幕尺寸的dimens值。下面将详细阐述这个工具的工作原理、使用方法以及它在Android应用适配中的作用...

    Android常用资源Map

    【标题】"Android常用资源Map"涉及到的是在Android开发中如何有效地管理和使用资源,特别是与Map相关的组件和概念。在Android应用开发中,资源管理是非常关键的一环,它包括图片、字符串、布局文件等,而Map作为一种...

    android studio finished with non-zero exit value 1, value 2解决办法

    ### Android Studio 中非零退出值 (Non-Zero Exit Value) 错误详解及解决方法 #### 背景概述 在使用 Android Studio 进行应用开发的过程中,有时会遇到 `finished with non-zero exit value` 的错误提示。这类错误...

    android多种资源的链接

    8. 值资源(Value Resources):包括integers.xml(整数资源),arrays.xml(数组资源),styles.xml(样式资源)等,用于存储各种类型的数据。 9. 动态链接库(Library Projects):Android Library项目允许开发者...

    Android中英语言切换资源res.rar

    这个"Android中英语言切换资源res.rar"压缩包包含了实现这一功能所需的关键资源。资源通常存储在项目的`res`目录下,这个目录是Android Studio项目中管理应用各种资源如图片、布局、字符串等的地方。 在Android中,...

    Android 多国语言value文件夹命名的方法

    在Android应用开发中,为了实现多国语言支持,开发者需要创建特定的`values`文件夹来存放不同语言的资源文件。这些文件夹的命名规则是关键,因为它们决定了系统如何根据用户设备的语言设置来选择相应的资源。以下是...

    CMDN CLUB#14期:Android系统资源访问机制的探讨

    资源的定义方式还包括了属性(attr)、样式(style)、值类型(valuetype)等,这些定义在XML中的结构为应用提供了灵活的资源使用方式。比如,TextView控件在平台的attrs.xml中就定义了各种属性,如bufferType、text...

    Android颜色资源文件color.xml

    颜色齐全,并且包含中文颜色注释,可直接复制到项目工程中使用

    Android端USB连接示例

    这个示例代码是学习和实践Android USB连接功能的宝贵资源,通过阅读和理解代码,开发者可以更好地掌握USB设备在Android上的操作。 总之,Android端USB连接涉及到多个步骤和类的交互,理解并实践这些知识点是开发USB...

    Android 之 资源文件的介绍及使用

    3. 值资源(Value Resources):位于res/values目录下,用于定义字符串、颜色、尺寸、样式等,如strings.xml、colors.xml、styles.xml。 4. 动态布局(Data Binding):利用data binding库,可以在布局XML中直接...

    消除 Android 项目 Key-Value 样板代码.zip

    为了满足广大Android开发爱好者与从业者的学习需求,我们精心整理并上传了一份全面而实用的Android项目资源包。这份资源包内容丰富,涵盖了从基础知识到实战应用的全方位内容,旨在为开发者们提供一个便捷、高效的...

    Android values文件

    在Android应用开发中,`values`文件夹是一个至关重要的组件,它存储了应用程序中的各种资源定义,特别是与字符串、颜色、尺寸和样式等相关的资源。`values`文件夹位于项目的`res`目录下,它是Android Studio工程结构...

    qt for android 实现Toast弹窗,本地通知栏显示,弹出在后台的界面以及加入资源系统白名单

    在本文中,我们将深入探讨如何在Qt for Android环境中实现Toast弹窗、本地通知栏显示、在后台弹出界面以及加入资源系统白名单等关键功能。 首先,让我们详细了解`Qt for Android`。Qt for Android是Qt库的一个扩展...

    android获取网络资源demo

    在Android开发中,获取网络资源是一项常见的任务,尤其在当今移动互联网时代,应用程序与服务器的交互至关重要。本示例“android获取网络资源demo”聚焦于如何利用URL和HTTP协议从远程服务器获取数据。我们将深入...

    Android按顺序的放大缩小动画

    android:valueFrom="1.0" android:valueTo="1.2" android:startOffset="0" /&gt; android:duration="500" android:propertyName="scaleY" android:valueFrom="1.0" android:valueTo="1.2" android:...

    Android 在上个Android简单程序改进后可以传递数据到另外的组件中

    2. **添加额外数据**: 使用`putExtra()`方法向Intent中添加数据,例如`intent.putExtra("key", value)`,这里的"key"是标识符,"value"是要传递的数据。 3. **启动目标Activity**: 使用`startActivity(Intent)`方法...

    Android控件点击变色

    android:valueTo="@color/button_pressed_color" /&gt; ``` ```java ObjectAnimator colorChange = AnimatorInflater.loadAnimator(context, R.animator.button_color_change); colorChange.setTarget(myButton);...

    android实现网页链接

    例如,可以使用XML资源文件定义样式,或者在Java代码中动态设置属性。同时,利用selector(选择器)可以根据Button的状态(如按下、焦点、默认状态)改变其显示效果。 接下来是RadioButton的使用。RadioButton属于...

    Android带动态效果的Button(按钮)

    最后,对于更复杂的动画效果,如涟漪效果,可以使用`ripple`资源类型,这在Android 5.0(API 21)及以上版本可用。创建一个`res/drawable/button_ripple.xml`: ```xml &lt;ripple xmlns:android=...

    android Material揭露动画

    android:valueType="floatType" android_duration="100"/&gt; &lt;item android:state_pressed="false"&gt; android:propertyName="translationZ" android:valueTo="0dp" android:valueType="floatType" android_...

Global site tag (gtag.js) - Google Analytics