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. You need to place each resource in the project Catalogue Resource type A XML file that defines the properties of the animation. They are saved in the res/anim/ folder through the A XML file that defines a list of color states. They are saved in the res/color/ folder through the 图片文件,如.png,.jpg,.gif或者XML文件,被编译为位图、状态列表、形状、动画图片。它们被保存在 A XML file that defines the layout of the user interface. They are saved in the res/layout/ folder through the 定义应用程序菜单的XML文件,如选项菜单,上下文菜单,子菜单等。它们被保存在res/menu/文件夹下,通过R.menu类访问 Arbitrary files are saved in their original form. Need to be based on the name 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 You can call the 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 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. Here is another example that specifies the layout of the default language and the alternative layout of the Arabic language. 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. When the Android application is compiled, an R class is generated that contains all of the Visit The first line of code here uses Consider the next example, where Now you can work for ID Consider the layout defined below The application code will be Consider the following XML resource file Now, you can use these resources to set the text color and text content in the following layout file: 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. 3.9.1. Organize resources in eclipse ¶
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.
anim/
R.anim
Class access
color/
R.color
Class access
drawable/
res/drawable/
Under the folder, through the
R.drawable
Class access
layout/
R.layout
Class access
menu/
raw/
R.raw.filename
Resource ID, by calling the
Resource.openRawResource()
To open the raw file
values/
xml/
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 ¶
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.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
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 ¶
3.9.4. Access resources in code ¶
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 ¶
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);
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 ¶
res/values/strings.xml
There are the following definitions:<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello, World!</string>
</resources>
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 ¶
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>
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 ¶
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>
<?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" />