[Android]使用化名(alias)功能防止相同资源的重复_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > [Android]使用化名(alias)功能防止相同资源的重复

[Android]使用化名(alias)功能防止相同资源的重复

 2013/10/8 21:31:59  Ginsan  博客园  我要评论(0)
  • 摘要:在为一个应用匹配不同资源文件的时候,有时可能需要在不同适配类型的资源路径下使用相同的资源文件,这时使用alias方法可以防止相同资源文件的重复,提高效率。以下摘自Android开发文档http://developer.android.com/guide/topics/resources/providing-resources
  • 标签:android 功能 使用 资源

在为一个应用匹配不同资源文件的时候,有时可能需要在不同适配类型的资源路径下使用相同的资源文件,这时使用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.

class="note">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文件来替代本来需要在不同路径下重复存在的较大的资源文件,这适用于图片、布局文件、字符串等资源。
发表评论
用户名: 匿名