3.9. Android Resource (Resources) access

发布时间 :2025-10-25 12:24:11 UTC      

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:

  • In res/ Create a new directory under to <resource_name>_<config_qualifier> The way it is named. Here resources_name Is any of the resources mentioned in the above table, such as layouts, pictures, etc. qualifier It will determine which resources are used in the configuration of personality. You can check the official documentation for a complete picture of different types of resources. qualifier List.

  • Save alternative resources for responses in this directory. These resource files must match the default resource file names shown in the following example, but these files will replace what is determined. For example, although the file name of the picture is the same, the resolution of the picture will also be high on a high-resolution screen.

Here is an example that specifies a default screen image and a high-resolution alternative image.

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

Here is another example that specifies the layout of the default language and the alternative layout of the Arabic language.

MyProject/
   src/
    main/
    java/
       MyActivity.java
      res/
         drawable/
            icon.png
            background.png
        drawable-hdpi/
            icon.png
            background.png
        layout/
            activity_main.xml
            info.xml
        layout-ar/
            main.xml
        values/
            strings.xml

3.9.3. Access to resources

In application development, you need to access defined resources, either through code or through XML files. The following sections describe how to access resources in each of these two scenarios.

3.9.4. Access resources in code

When the Android application is compiled, an R class is generated that contains all of the res/ The ID of the resources under the directory. You can use the R class to access the resource through the subclass + resource name or directly using the resource ID.

3.9.5. Example

Visit res/drawable/myimage.png And set it to ImageView You will use the following code:

ImageView imageView = (ImageView) findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);

The first line of code here uses R.id.myimageview To get in the layout file defined as myimageview Of ImageView . The second line is used R.drawable.myimage To get access to the res/ Of drawable Under the subdirectory named myimage It’s a picture.

Example-1

Consider the next example, where res/values/strings.xml There are the following definitions:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string  name="hello">Hello, World!</string>
</resources>

Now you can work for ID msg Of TextView Use the resource ID on the object to set the text, as follows:

TextView msgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.hello);

Example-2

Consider the layout defined below res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

   <TextView android:id="@+id/text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Hello, I am a TextView" />

   <Button android:id="@+id/button"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Hello, I am a Button" />

</LinearLayout>

The application code will be activity Load this layout onCreate() The method is as follows:

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main_activity);
}

3.9.6. Access in XML

Consider the following XML resource file res/values/strings.xml Which contains a color resource and a string resource-

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="opaque_red">#f00</color>
   <string name="hello">Hello!</string>
</resources>

Now, you can use these resources to set the text color and text content in the following layout file:

<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textColor="@color/opaque_red"
    android:text="@string/hello" />

Now, if you go back to the “Hello World!” example explained in the previous chapter, I can be sure that you have a better understanding of all the concepts in this section. Therefore, I strongly recommend going back to the previous example and looking at the basic usage of my different resources.

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.