概括地讲,Android中资源访问是指非代码的部分。例如在我们的Android程序中要使用一些图片来设置桌面,要使用一些音频设置铃声,要使用一些动画来显示特效,这个图片,音频,动画等就叫做Android中的资源文件。
我们在创建Android工程时,和src文件夹并列的有两个文件夹:assets和res。这两个文件夹用来保存资源文件。在assets保存的一般都是原生的文件,例如Map3文件,android程序是不能直接访问的,必须通过AssetManager类以二进制流的形式读入。而res中的资源经常访问,可以通过R类直接访问。res目录下又有anim,drawable,layout,menu,raw,values和xml文件夹,分别存放动画,图片,布局文件,菜单,原生文件,常量值和xml文件。
在Android中,我们使用Context的getResource()方法得到Resources对象,该对象提供了获得各种类型资源的方法。在其它资源中引用资源的一般格式是这样的:@[包名称:]资源类型/资源名称,例如,下面的代码是在布局文件中引用颜色、字符串资源和尺寸资源:
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:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:textColor="@color/Red" android:textSize="@dimen/normal" /> </LinearLayout> strings.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, HelloAndroidActivity!</string> <string name="app_name">HelloAndroid</string> <string name="title">This is the title</string> </resources> color.xml <?xml version="1.0" encoding="utf-8"?> <resources> <color name="Red">#f00</color> <color name="Blue">#0000ff</color> </resources>
|
资源的类型和布局
我们可以在res目录下创建和保存各种不同类型的资源文件。这些资源文件要有一定的格式。Android使用资源编辑器工具AAPT来编译这些资源文件
目录结构 | 资源类型 |
res/anim/ | XML动画文件 |
res/drawable/ | 一些位图文件 |
res/layout | XML布局文件 |
res/values/ | 各种XML资源文件 arrays.xml:XML数据文件 colors.xml:XML颜色文件 dimens.xml:XML尺寸文件 styles.xml:XML样式文件 strings.xml:XML字符串文件 |
res/xml/ | 任意的XML文件 |
res/raw/ | 直接复制到设备中的原生文件 |
res/menu/ | XML菜单文件 |
资源文件的使用:
在我们编译一个Android应用程序时,Android会自动生成一个R类
/* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */ package com.synvata.HelloAndroid; public final class R { public static final class attr { } public static final class color { public static final int Blue=0x7f040001; public static final int Red=0x7f040000; } public static final class dimen { public static final int big=0x7f050001; public static final int biger=0x7f050002; public static final int normal=0x7f050000; } public static final class drawable { public static final int ic_launcher=0x7f020000; } public static final class layout { public static final int main=0x7f030000; } public static final class string { public static final int app_name=0x7f060001; public static final int hello=0x7f060000; } }
每一个资源都有一个唯一的整数与之相对应。在代码中使用R资源类访问的具体格式为:R.资源文件类型.资源文件名称
//设置Activity显示的布局试图 setContentView(R.layout.main); //设置Activity的标题,使用getResources()获取资源, //getResources().getDrawable(id) 一些位图文件 //getResources().getString(id) 字符串资源 //getResources().getXml(id) 任意的XML文件 //....... //getResources()方法可以获取各种资源,与上面资源类型相对应,并接受一个整数参数来获取资源。该整数就是R类中资源对应的数值。 setTitle("应用的标题为:"+ getResources().getString(R.string.title));
另外,除了访问用户自定义的资源文件,还可以访问系统中的资源文件。大部分的资源文件定义在android包下的R类中,访问系统中的资源文件格式为:android.R.资源文件类型/资源文件名称
int i; //获取android中cancel对应的字符串 i=android.R.string.cancel;