Android 简明教程
Android Resources Organizing & Accessing
还有很多用于构建一个优秀的 Android 应用的其他内容。除了为应用进行编码外,你还可以处理其他 resources ,例如你的代码使用的静态内容(例如位图、颜色、布局定义、用户界面字符串、动画说明等)。这些资源总是单独保存在项目 res/ 目录下的多个子目录中。
There are many more items which you use to build a good Android application. Apart from coding for the application, you take care of various other resources like static content that your code uses, such as bitmaps, colors, layout definitions, user interface strings, animation instructions, and more. These resources are always maintained separately in various sub-directories under res/ directory of the project.
本教程将向你解释如何组织你的应用资源、指定备用资源以及在你的应用中访问它们。
This tutorial will explain you how you can organize your application resources, specify alternative resources and access them in your applications.
Organize resource in Android Studio
MyProject/
app/
manifest/
AndroidManifest.xml
java/
MyActivity.java
res/
drawable/
icon.png
layout/
activity_main.xml
info.xml
values/
strings.xml
Sr.No. |
Directory & Resource Type |
1 |
anim/ XML files that define property animations. They are saved in res/anim/ folder and accessed from the R.anim class. |
2 |
color/ XML files that define a state list of colors. They are saved in res/color/ and accessed from the R.color class. |
3 |
drawable/ Image files like .png, .jpg, .gif or XML files that are compiled into bitmaps, state lists, shapes, animation drawable. They are saved in res/drawable/ and accessed from the R.drawable class. |
4 |
layout/ XML files that define a user interface layout. They are saved in res/layout/ and accessed from the R.layout class. |
5 |
menu/ XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. They are saved in res/menu/ and accessed from the R.menu class. |
6 |
raw/ Arbitrary files to save in their raw form. You need to call Resources.openRawResource() with the resource ID, which is R.raw.filename to open such raw files. |
7 |
values/ XML files that contain simple values, such as strings, integers, and colors. For example, here are some filename conventions for resources you can create in this directory − arrays.xml for resource arrays, and accessed from the R.array class. integers.xml for resource integers, and accessed from the R.integer class. bools.xml for resource boolean, and accessed from the R.bool class. colors.xml for color values, and accessed from the R.color class. dimens.xml for dimension values, and accessed from the R.dimen class. strings.xml for string values, and accessed from the R.string class. styles.xml for styles, and accessed from the R.style class. |
8 |
xml/ Arbitrary XML files that can be read at runtime by calling Resources.getXML(). You can save various configuration files here which will be used at run time. |
Alternative Resources
应用程序应该提供替代资源来支持特定的设备配置。例如,您应该为不同的屏幕分辨率包含替代的 drawable 资源(即图像)以及为不同的语言包含替代的字符串资源。在运行时,Android 会检测当前的设备配置并加载应用程序的适当资源。
Your application should provide alternative resources to support specific device configurations. For example, you should include alternative drawable resources ( i.e.images ) for different screen resolution and alternative string resources for different languages. At runtime, Android detects the current device configuration and loads the appropriate resources for your application.
要为一组资源指定特定于配置的替代方案,请按照以下步骤操作 -
To specify configuration-specific alternatives for a set of resources, follow the following steps −
-
Create a new directory in res/ named in the form <resources_name>-<config_qualifier>. Here resources_name will be any of the resources mentioned in the above table, like layout, drawable etc. The qualifier will specify an individual configuration for which these resources are to be used. You can check official documentation for a complete list of qualifiers for different type of resources.
-
Save the respective alternative resources in this new directory. The resource files must be named exactly the same as the default resource files as shown in the below example, but these files will have content specific to the alternative. For example though image file name will be same but for high resolution screen, its resolution will be high.
以下是一个示例,它指定了默认屏幕的图像和高分辨率屏幕的替代图像。
Below is an example which specifies images for a default screen and alternative images for high resolution screen.
MyProject/
app/
manifest/
AndroidManifest.xml
java/
MyActivity.java
res/
drawable/
icon.png
background.png
drawable-hdpi/
icon.png
background.png
layout/
activity_main.xml
info.xml
values/
strings.xml
以下是一个示例,它指定了默认语言的布局和阿拉伯语的替代布局。
Below is another example which specifies layout for a default language and alternative layout for Arabic language.
MyProject/
app/
manifest/
AndroidManifest.xml
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
Accessing Resources
在应用程序开发过程中,您需要在代码或布局 XML 文件中访问已定义的资源。以下部分说明如何在两种情况下访问资源 -
During your application development you will need to access defined resources either in your code, or in your layout XML files. Following section explains how to access your resources in both the scenarios −
Accessing Resources in Code
当编译 Android 应用程序时,将生成一个 R 类,其中包含 res/ 目录中所有可用资源的资源 ID。您可以使用 R 类使用子目录和资源名称或直接使用资源 ID 访问该资源。
When your Android application is compiled, a R class gets generated, which contains resource IDs for all the resources available in your res/ directory. You can use R class to access that resource using sub-directory and resource name or directly resource ID.
Example
要访问 res/drawable/myimage.png 并设置 ImageView,您将使用以下代码 -
To access res/drawable/myimage.png and set an ImageView you will use following code −
ImageView imageView = (ImageView) findViewById(R.id.myimageview);
imageView.setImageResource(R.drawable.myimage);
此处,代码的第一行使用 R.id.myimageview 在布局文件中通过 id myimageview 定义的 ImageView。代码的第二行使用 R.drawable.myimage 获取 myimage 名称的图像,该名称可在 /res 下的 drawable 子目录中找到。
Here first line of the code make use of R.id.myimageview to get ImageView defined with id myimageview in a Layout file. Second line of code makes use of R.drawable.myimage to get an image with name myimage available in drawable sub-directory under /res.
Example
考虑以下示例,其中 res/values/strings.xml 有以下定义 -
Consider next example where res/values/strings.xml has following definition −
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello, World!</string>
</resources>
现在,您可以使用资源 ID 来设置具有 ID msg 的 TextView 对象上的文本,如下所示 -
Now you can set the text on a TextView object with ID msg using a resource ID as follows −
TextView msgTextView = (TextView) findViewById(R.id.msg);
msgTextView.setText(R.string.hello);
Example
考虑具有以下定义的布局 res/layout/activity_main.xml -
Consider a layout res/layout/activity_main.xml with the following definition −
<?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>
此应用程序代码将在 onCreate() 方法中加载此布局以用于 Activity,如下所示 -
This application code will load this layout for an Activity, in the onCreate() method as follows −
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Accessing Resources in XML
考虑以下包含颜色资源和字符串资源的资源 XML 文件 res/values/strings.xml -
Consider the following resource XML res/values/strings.xml file that includes 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 in the following layout file to set the text color and text string as follows −
<?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" />
现在,如果您再次阅读上一章,其中我解释了 Hello World! 示例,我相信您会对本章中解释的所有概念有更好的理解。因此,我强烈建议查看上一章以了解工作示例,并了解如何在最基本的层次上使用各种资源。
Now if you will go through previous chapter once again where I have explained Hello World! example, and I’m sure you will have better understanding on all the concepts explained in this chapter. So I highly recommend to check previous chapter for working example and check how I have used various resources at very basic level.