转自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.
- this .getWindow().setBackgroundDrawableResource(R.drawable.my_background_image);
- // WRONG Sending a string resource reference into a
- // method that expects a string.
- this .getWindow().setTitle(R.string.main_title);
- // RIGHT Need to get the title from the Resources wrapper.
- this .getWindow().setTitle(Resources.getText(R.string.main_title));
- // Load a custom layout for the current screen.
- setContentView(R.layout.main_screen);
- // Set a slide in animation for a ViewFlipper object.
- mFlipper.setInAnimation(AnimationUtils.loadAnimation(this ,
- R.anim.hyperspace_in));
- // Set the text on a TextView object.
- TextView msgTextView = (TextView)findViewByID(R.id.msg);
- msgTextView.setText(R.string.hello_message);
- //在屏幕上显示标准应用程序的图标
- public class MyActivity extends Activity {
- public void onStart() {
- requestScreenFeatures(FEATURE_BADGE_IMAGE);
- super .onStart();
- setBadgeResource(android.R.drawable.sym_def_app_icon);
- }
- }
- //应用系统定义的标准"绿色背景"视觉处理
- public class MyActivity extends Activity
- public void onStart() {
- super .onStart();
- setTheme(android.R.style.Theme_Black);
- }
- }
4. xml文件内引用资源
- <? xml version = "1.0" encoding = "utf-8" ?>
- < resources >
- < string name = "hello" > Hello World, HelloDemo! </ string >
- </ resources >
5. 替换资源(为了可替换的资源和配置)
6. Color Value
- < color name =" color_name" > #color_value </ color >
- <? xml version = "1.0" encoding = "utf-8" ?>
- < resources >
- < color name = "opaque_red" > #f00 </ color >
- < color name = "translucent_red" > #80ff0000 </ color >
- </ resources >
7.Color Drawables
- < drawable name =" color_name" > color_value </ drawable >
- <? xml version = "1.0" encoding = "utf-8" ?>
- < resources >
- < drawable name = "opaque_red" > #f00 </ drawable >
- < drawable name = "translucent_red" > #80ff0000 </ drawable >
- </ 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" ?>
- < resources >
- < dimen name = "one_pixel" > 1px </ dimen >
- < dimen name = "double_density" > 2dp </ dimen >
- < dimen name = "sixteen_sp" > 16sp </ dimen >
- </ resources >
10. string
- //不使用转义符则需要用双引号包住整个string
- < string name = "good_example" > "This'll work" </ string >
- //使用转义符
- < string name = "good_example_2" > This/'ll also work </ string >
- //错误
- < string name = "bad_example" > This won't work! </ string >
- //错误 不可使用html转义字符
- < string name = "bad_example_2" > XML encodings won't work either! </ string >
- <? xml version = "1.0" encoding = "utf-8" ?>
- < resources >
- < string name = "simple_welcome_message" > Welcome! </ string >
- < string name = "styled_welcome_message" > We are < b > < i > so </ i > </ b > glad to see you. </ string >
- </ resources >
- < TextView
- android:layout_width = "fill_parent"
- android:layout_height = "wrap_content"
- android:textAlign = "center"
- android:text ="@string/simple_welcome_message " />
- // Assign a styled string resource to a TextView on the current screen.
- CharSequence str = getString(R.string.styled_welcome_message);
- TextView tv = (TextView)findViewByID(R.id.text);
- tv.setText(str);
- <? xml version = "1.0" encoding = "utf-8" ?>
- < resources >
- < string name = "search_results_resultsTextFormat" > %1$d results for < b > &quot;%2$s&quot; </ b > </ string >
- </ resources >
- //title是我们想赋值给%2$s的字符串
- String escapedTitle = TextUtil.htmlEncode(title);
- String resultsTextFormat = getContext().getResources().getString(R.string.search_results_resultsTextFormat);
- String resultsText = String.format (resultsTextFormat, count, escapedTitle);
- CharSequence styledResults = Html.fromHtml (resultsText);