3.9. Android Resource (Resources) access

发布时间 : 2025-10-25 13:33:23 UTC      

Page Views: 9 views

There are many things to build a good Android application. In addition to application coding, you need to focus on a variety of resources, such as the various static content you use, such as bitmaps, colors, layout definitions, user interface strings, animations, and so on. These resources are generally placed in the project’s res/ In an independent subdirectory.

This tutorial will learn how to organize application resources, specify alternative resources, and access them in the application.

3.9.1. Organize resources in eclipse

You need to place each resource in the project res/ A specific subdirectory of the directory. For example, this is the file level of a simple project:

MyProject/ src/ MyActivity.java res/ drawable/ icon.png layout/ activity_main.xml info.xml values/ strings.xml 

res/ The directory contains all the resources in various subdirectories. There is a picture resource, two layout resources and a string resource file. The following table shows in detail in the project res/ Resources supported in the directory.

Catalogue

Resource type

anim/

A XML file that defines the properties of the animation. They are saved in the res/anim/ folder through the R.anim Class access

color/

A XML file that defines a list of color states. They are saved in the res/color/ folder through the R.color Class access

drawable/

图片文件,如.png,.jpg,.gif或者XML文件,被编译为位图、状态列表、形状、动画图片。它们被保存在 res/drawable/ Under the folder, through the R.drawable Class access

layout/

A XML file that defines the layout of the user interface. They are saved in the res/layout/ folder through the R.layout Class access

menu/

定义应用程序菜单的XML文件,如选项菜单,上下文菜单,子菜单等。它们被保存在res/menu/文件夹下,通过R.menu类访问

raw/

Arbitrary files are saved in their original form. Need to be based on the name R.raw.filename Resource ID, by calling the Resource.openRawResource() To open the raw file

values/

An XML file that contains simple values such as strings, integers, colors, etc. Here are some resource naming conventions under the folder. Arrays.xml represents array resources, accessed through R.array class; integers.xml represents integer resources, accessed through R.integer class; bools.xml represents Boolean resources, accessed through R.bool class; colors.xml represents color resources, accessed through R.color class; dimens.xml represents dimension value, accessed through R.dimen class; strings.xml represents string resources, accessed through R.string class Styles.xml stands for style resources and is accessed through the R.style class

xml/

You can call the Resources.getXML() To read any XML file at run time. You can save various configuration files used at run time here

3.9.2. Alternative resources

Your application needs to provide alternative resource support for specific device configurations. For example, you need to provide alternative image resources for different screen resolutions and alternative string resources for different languages. At run time, Android detects the current device configuration and loads the appropriate resources for the application.

To identify a series of alternative resources for a specific configuration, follow these steps: