Android 资源别名
资源别名的常用场景:
例如 程序支持多国家和多语言,在不同的国家和语言使用不同的应用程序图标(icon.png),但是在
English-Canadian 和French-Canadian 情况下要使用相同的图标, 简单的方式就是创建对应的资源目
录 res/drawable-en-rCA/ 和 res/drawable-fr-rCA/ 然后把图标分别放到每个目录下. 这样有个缺
点 就是有2个一样的资源图标. 使用资源别名可以避免这种问题.
注意: 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.
下面分别演示了各种资源的别名如何创建
<!-- Drawable 资源别名 要创建Drawable资源别名 可以使用 bitmap 元素:如下 文件名为icon.xml --><?xml version="1.0" encoding="utf-8"?><bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/icon_ca" /><!--这样就可以通过R.drawable.icon来引用这个别名图标,实际上是指向默认目录中的icon_ca.png的 --><!--Layout别名 使用 merge 和 include 来创建 --><?xml version="1.0" encoding="utf-8"?><merge> <include layout="@layout/main_ltr"/></merge><!-- 字符串和其他简单的值 --><?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello</string> <string name="hi">@string/hello</string></resources><?xml version="1.0" encoding="utf-8"?><resources> <color name="yellow">#f00</color> <color name="highlight">@color/red</color></resources>