在为一个应用匹配不同资源文件的时候,有时可能需要在不同适配类型的资源路径下使用相同的资源文件,这时使用alias方法可以防止相同资源文件的重复,提高效率。以下摘自Android开发文档http://developer.android.com/guide/topics/resources/providing-resources.html#AliasResources

Creating alias resources

When you have a resource that you'd like to use for more than one device configuration (but do not want to provide as a default resource), you do not need to put the same resource in more than one alternative resource directory. Instead, you can (in some cases) create an alternative resource that acts as an alias for a resource saved in your default resource directory.

Note: Not all resources offer a mechanism by which you can create an alias to another resource. In particular, animation, menu, raw, and other unspecified resources in the xml/ directory do not offer this feature.

For example, imagine you have an application icon, icon.png, and need unique version of it for different locales. However, two locales, English-Canadian and French-Canadian, need to use the same version. You might assume that you need to copy the same image into the resource directory for both English-Canadian and French-Canadian, but it's not true. Instead, you can save the image that's used for both as icon_ca.png (any name other than icon.png) and put it in the default res/drawable/ directory. Then create an icon.xml file in res/drawable-en-rCA/ and res/drawable-fr-rCA/ that refers to the icon_ca.png resource using the <bitmap> element. This allows you to store just one version of the PNG file and two small XML files that point to it. (An example XML file is shown below.)

Drawable

To create an alias to an existing drawable, use the <bitmap> element. For example:

<?xml version="1.0" encoding="utf-8"?>
<bitmapxmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/icon_ca"/>

If you save this file as icon.xml (in an alternative resource directory, such as res/drawable-en-rCA/), it is compiled into a resource that you can reference as R.drawable.icon, but is actually an alias for the R.drawable.icon_ca resource (which is saved in res/drawable/).

Layout

To create an alias to an existing layout, use the <include> element, wrapped in a <merge>. For example:

<?xml version="1.0" encoding="utf-8"?>
<merge>
<includelayout="@layout/main_ltr"/>
</merge>

If you save this file as main.xml, it is compiled into a resource you can reference as R.layout.main, but is actually an alias for the R.layout.main_ltr resource.

Strings and other simple values

To create an alias to an existing string, simply use the resource ID of the desired string as the value for the new string. For example:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<stringname="hello">Hello</string>
<stringname="hi">@string/hello</string>
</resources>

The R.string.hi resource is now an alias for the R.string.hello.

Other simple values work the same way. For example, a color:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<colorname="yellow">#f00</color>
<colorname="highlight">@color/red</color>
</resources>

这样就可以用非常小的xml文件来替代本来需要在不同路径下重复存在的较大的资源文件,这适用于图片、布局文件、字符串等资源。

更多相关文章

  1. ionic3文件目录介绍
  2. android从未安装的apk文件里获取信息(包信息,资源信息)
  3. 提高开发效率-使用Android Studio Template快速生成模板文件
  4. Android API开发之OpenGL开发之Android OpenGL显示STL模型文件
  5. Github开源Android资源整理(十一)优秀项目
  6. 【android】转载:实用Android开发工具和资源精选
  7. android studio R文件找不到
  8. Android 系统资源(字体,声音,视频)的简介
  9. 解决IE apk变成zip:Android 手机应用程序文件下载服务器 配置解决

随机推荐

  1. Android(安卓)去掉底部虚拟导航栏
  2. android EditText 控件中加入图片(非背景
  3. Android调用系统功能获取当前经纬度
  4. 【Android】Activity全屏设置
  5. Android(安卓)HttpClient网络通信
  6. Android(安卓)TelephonyManager类用法
  7. Android(安卓)WebView 加载网页遇到的问
  8. android写入任意路径中
  9. Android(安卓)ProgressDialog设置透明程
  10. android调用本地录制程序获取录制文件路