[Android]使用化名(alias)功能防止相同资源的重复
在为一个应用匹配不同资源文件的时候,有时可能需要在不同适配类型的资源路径下使用相同的资源文件,这时使用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文件来替代本来需要在不同路径下重复存在的较大的资源文件,这适用于图片、布局文件、字符串等资源。
[Android]使用化名(alias)功能防止相同资源的重复的更多相关文章
- Android必会小功能总结
1.获取屏幕尺寸.密度等信息. 1)最常用的方法: WindowManager windowManager = getWindowManager(); Display display = window ...
- 你真的有必要退出吗——再说Android程序的退出功能
转自你真的有必要退出吗--再说Android程序的退出功能 搞Android开发有一段时间了,相信很多从Windows开发过来的Android程序员都习惯性地会跟我一样遇到过同一个问题:如何彻底退出程 ...
- Android开发人员必知的开发资源
developer.android.com 官方开发人员网站推荐资源 在动手编写第一个 Android 应用之前,用心读一读 Android Design 章节.尤其是以下的这些文章: Devices ...
- Android 7.0 新增功能和api
Android 7.0 Nougat 为用户和开发者引入多种新功能.本文重点介绍面向开发者的新功能. 请务必查阅 Android 7.0 行为变更以了解平台变更可能影响您的应用的领域. 要详细了解 A ...
- android源码framework下添加新资源的方法
编译带有资源的jar包,需要更改frameworks层,方法如下: 一.增加png类型的图片资源 1.将appupdate模块所有用到的png格式图片拷贝到framework/base/core/re ...
- Android Webview实现文件下载功能
在做美图欣赏Android应用的时候,其中有涉及到Android应用下载的功能,这个应用本身其实也比较简单,就是通过WebView控制调用相应的WEB页面进行展示.刚开始以为和普通的文件下载实 ...
- I.MX6 android 移除shutdown功能
/************************************************************************ * I.MX6 android 移除shutdown ...
- 调用Android自带日历功能(日历列表单、添加一个日历事件)
调用Android自带日历功能 觉得这篇文章不错,转载过来. 转载:http://blog.csdn.net/djy1992/article/details/9948393 Android手机配备有 ...
- Android使得手机拍照功能的发展(源共享)
Android系统调用手机拍照功能有两种方法来直接调用手机自带摄像头还有一个就是要当心自己的节拍. 例Camera360 强大的一个在每个操作系统都有一个手机摄影软件:您可以捕捉不同风格,不同特效的照 ...
随机推荐
- 14. Spring Boot MyBatis 连接数据库
转自:https://blog.csdn.net/catoop/article/details/50553714
- 【Codeforces Round #433 (Div. 2) C】Planning
[链接]h在这里写链接 [题意] 让你确定ti,使得∑(ti-i)*gi最小,其中ti∈[k+1..k+n],且每个ti都不能一样. 且ti>=i必须成立. [题解] 分解一下成为∑ti*gi ...
- Java基础学习总结(31)——Java思维导图
- 【Redis】redis的安装、配置执行及Jedisclient的开发使用
定义: Redis is an open source, BSD licensed, advanced key-value cache and store. It is often referred ...
- [Vue] Load components when needed with Vue async components
In large applications, dividing the application into smaller chunks is often times necessary. In thi ...
- 10.8 android输入系统_实战_使用GlobalKey一键启动程序
11. 实战_使用GlobalKey一键启动程序参考文章:Android 两种注册(动态注册和静态注册).发送广播的区别http://www.jianshu.com/p/ea5e233d9f43 [A ...
- 使用doxygen为C/C++程序生成中文文档
文章来自:http://www.fmddlmyy.cn/text21.html 依照约定的格式凝视源码,用工具处理凝视过的源码产生文档.通过这样的方式产生文档至少有下面优点: 便于代码和文档保持同步. ...
- [Maven实战](6)仓库(本地仓库,远程仓库,镜像)
1. 简单介绍 maven能够在某个位置统一存储全部maven项目共享的构件,这个统一的位置就是仓库.实际的Maven项目将不会各自存储其依赖文件,它们仅仅须要声明这些依赖的坐标,在须要的时候(比如. ...
- Swift 中异常抛出和四种异常处理
在Swift中你可以像其他语言一样抛出异常处理异常,今天我们就详细地说说Swift中的异常抛出和处理. 在一开始我们要定义错误或者说是异常,Swift中的一些简单异常可以使用枚举定义,注意这个枚举要继 ...
- php 下载图片到服务器
function saveImage($path) { if(!preg_match('/\/([^\/]+\.[a-z]{3,4})$/i',$path,$matches)) die('Use im ...