原文出处:http://android.blog.51cto.com/268543/302529
1. 相关文件夹介绍
目录Directory | 资源类型Resource Types |
res/anim/ | XML文件,它们被编译进逐帧动画(frame by frame animation)或补间动画(tweened animation)对象 |
res/drawable/ | .png、.9.png、.jpg文件,它们被编译进以下的Drawable资源子类型中: 要获得这种类型的一个资源,可以使用Resource.getDrawable(id) 为了获取资源类型,使用mContext.getResources().getDrawable(R.drawable.imageId) 注意:放在这里的图像资源可能会被aapt工具自动地进行无损压缩优化。比如,一个真彩色但并不需要256色的PNG可能会被转换为一个带调色板的8位PNG。这使得同等质量的图片占用更少的资源。所以我们得意识到这些放在该目录下的二进制图像在生成时可能会发生变化。如果你想读取一个图像位流并转换成一个位图(bitmap),请把图像文件放在res/raw/目录下,这样可以避免被自动优化。 |
res/layout/ | 被编译为屏幕布局(或屏幕的一部分)的XML文件。参见布局声明(Declaring Layout) |
res/values/ | 可以被编译成很多种类型的资源的XML文件。 注意:不像其他的res/文件夹,它可以保存任意数量的文件,这些文件保存了要创建资源的描述,而不是资源本身。XML元素类型控制这些资源应该放在R类的什么地方。 尽管这个文件夹里的文件可以任意命名,不过下面使一些比较典型的文件(文件命名的惯例是将元素类型包含在该名称之中): array.xml 定义数组 colors.xml 定义color drawable和颜色的字符串值(color string values)。使用Resource.getDrawable()和Resources.getColor()分别获得这些资源。 dimens.xml定义尺寸值(dimension value)。使用Resources.getDimension()获得这些资源。 strings.xml定义字符串(string)值。使用Resources.getString()或者Resources.getText()获取这些资源。getText()会保留在UI字符串上应用的丰富的文本样式。 styles.xml 定义样式(style)对象。 |
res/xml/ | 任意的XML文件,在运行时可以通过调用Resources.getXML()读取。 |
res/raw/ | 直接复制到设备中的任意文件。它们无需编译,添加到你的应用程序编译产生的压缩文件中。要使用这些资源,可以调用Resources.openRawResource(),参数是资源的ID,即R.raw.somefilename。 |
2.自动生成的R class
3. 在代码中使用资源
// Load a background for the current screen from a drawable resource.
02.this.getWindow().setBackgroundDrawableResource(R.drawable.my_background_image);
03.
04.// WRONG Sending a string resource reference into a
05.// method that expects a string.
06.this.getWindow().setTitle(R.string.main_title);
07.
08.// RIGHT Need to get the title from the Resources wrapper.
09.this.getWindow().setTitle(Resources.getText(R.string.main_title));
10.
11.// Load a custom layout for the current screen.
12.setContentView(R.layout.main_screen);
13.
14.// Set a slide in animation for a ViewFlipper object.
15.mFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
16. R.anim.hyperspace_in));
17.
18.// Set the text on a TextView object.
19.TextView msgTextView = (TextView)findViewByID(R.id.msg);
20.msgTextView.setText(R.string.hello_message);
查了SDK Doc,才明白为什么window.setTitle要先Resources.getText,原来setTitle的参数是CharSequence,Resources.getText(int)返回的是CharSequence;而其他setText的参数有的是CharSequence,有的是int(这就是Resources变量值)。
同时官方还给了两个使用系统资源的例子:
//在屏幕上显示标准应用程序的图标
02.public class MyActivity extends Activity {
03. public void onStart() {
04. requestScreenFeatures(FEATURE_BADGE_IMAGE);
05. super.onStart();
06. setBadgeResource(android.R.drawable.sym_def_app_icon);
07. }
08.}
09.
10.//应用系统定义的标准"绿色背景"视觉处理
11.public class MyActivity extends Activity
12. public void onStart() {
13. super.onStart();
14. setTheme(android.R.style.Theme_Black);
15. }
16.}
4. xml文件内引用资源
<?xml version="1.0" encoding="utf-8"?>
02.<resources>
03. <string name="hello">Hello World, HelloDemo!</string>
04.</resources>
5. 替换资源(为了可替换的资源和配置)
6. Color Value
- <color name="color_name">#color_value</color>
<?xml version="1.0" encoding="utf-8"?>
02.<resources>
03. <color name="opaque_red">#f00</color>
04. <color name="translucent_red">#80ff0000</color>
05.</resources>
7.Color Drawables
<drawable name="color_name">color_value</drawable>
xml示例:
<?xml version="1.0" encoding="utf-8"?>
02.<resources>
03. <drawable name="opaque_red">#f00</drawable>
04. <drawable name="translucent_red">#80ff0000</drawable>
05.</resources>
8. 图片
9. dimension
- <dimen name="dimen_name">dimen_value单位</dimen>
Java: float dimen = Resources.getDimen(R.dimen.some_name)
<?xml version="1.0" encoding="utf-8"?>
02.<resources>
03. <dimen name="one_pixel">1px</dimen>
04. <dimen name="double_density">2dp</dimen>
05. <dimen name="sixteen_sp">16sp</dimen>
06.</resources>
10. string
//不使用转义符则需要用双引号包住整个string
02.<string name="good_example">"This'll work"</string>
03.
04.//使用转义符
05.<string name="good_example_2">This\'ll also work</string>
06.
07.//错误
08.<string name="bad_example">This won't work!</string>
09.
10.//错误 不可使用html转义字符
11.<string name="bad_example_2">XML encodings won't work either!</string>
对于带格式的string,例如在字符串中某些文字设置颜色,可以使用html标签。对于这类型的string,需要进行某些处理,在xml里面不可以被其他资源引用。官方给了一个例子来对比普通string和带格式string的使用:
<?xml version="1.0" encoding="utf-8"?>
02.<resources>
03. <string name="simple_welcome_message">Welcome!</string>
04. <string name="styled_welcome_message">We are <b><i>so</i></b> glad to see you.</string>
05.</resources>
Xml代码<pre class="html" name="code"><TextView
02. android:layout_width="fill_parent"
03. android:layout_height="wrap_content"
04. android:textAlign="center"
05. android:text="@string/simple_welcome_message"/>
Java代码
01.// Assign a styled string resource to a TextView on the current screen.
02.CharSequence str = getString(R.string.styled_welcome_message);
03.TextView tv = (TextView)findViewByID(R.id.text);
04.tv.setText(str);
另外对于带风格/格式的string的处理,就麻烦一点点。官方给了一个例子 :
<?xml version="1.0" encoding="utf-8"?>
02.<resources>
03. <string name="search_results_resultsTextFormat">%1$d results for <b>"%2$s"</b></string>
04.</resources>
这里的%1$d是个十进制数字,%2$s是字符串。当我们把某个字符串赋值给%2$s之前,需要用htmlEncode(String)函数处理那个字符串:
//title是我们想赋值给%2$s的字符串
02.String escapedTitle = TextUtil.htmlEncode(title);
然后用String.format() 来实现赋值,接着用fromHtml(String) 得到格式化后的string:
String resultsTextFormat = getContext().getResources().getString(R.string.search_results_resultsTextFormat);
02.String resultsText = String.format(resultsTextFormat, count, escapedTitle);
03.CharSequence styledResults = Html.fromHtml(resultsText);