[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 强大的一个在每个操作系统都有一个手机摄影软件:您可以捕捉不同风格,不同特效的照 ...
随机推荐
- Codeforces Round #367 (Div. 2) (A,B,C,D,E)
Codeforces Round 367 Div. 2 点击打开链接 A. Beru-taxi (1s, 256MB) 题目大意:在平面上 \(n\) 个点 \((x_i,y_i)\) 上有出租车,每 ...
- css页面滚动条出现时防止页面跳动的方法
大家写页面时应该都遇到过一个问题,尤其是写单页面应用的时候, 在有滚动条页面和没有滚动条页面之间相互跳转时, 你页面的主体内容会向左或者向右抖一下,让强迫症看了很不舒服. 现在就来解救一下强迫症: 方 ...
- DC中为什么要用Uniquify?
转自:http://blog.sina.com.cn/s/blog_68c493870101exl7.html 为了在layout中进行时钟树的综合,网表在DC中必须被uniquified.所谓uni ...
- GO语言学习(七)Go 语言变量
Go 语言变量 变量来源于数学,是计算机语言中能储存计算结果或能表示值抽象概念.变量可以通过变量名访问. Go 语言变量名由字母.数字.下划线组成,其中首个字母不能为数字. 声明变量的一般形式是使用 ...
- 1、opencv3.3.0和cmake安装步骤(按照以下步骤安装后仅能在PC上运行,动态库也是PC端的属性)
1.下载安装CMake for Linux 下载地址:https://cmake.org/download/ 我下载的版本是,下载文件cmake-3.11.1.tar.gz ./bootstrap m ...
- iOS开发UI篇--一个侧滑菜单SlidingMenu
一.简介 侧滑菜单已经成为app一个极常用的设计,不管是事务类,效率类还是生活类app.侧滑菜单因Path 2.0和Facebook为开发者熟知,国内目前也有很多流行app用到了侧滑菜单,比如QQ.网 ...
- width:100%和width:inherit
前几天遇到过这么一个问题.我想让子盒子的宽度等于父盒子的宽度.父盒子宽度为一个具体值比如说200px.我将子盒子宽度设为了100%.按道理说应该是可以等于父盒子的宽度的,但结果并没有,而是通栏了.然后 ...
- svn 清理失败 (clean up 失败) 的解决方法
解决方法: step1: 到 sqlite官网 (http://www.sqlite.org/download.html) 下载 sqlite3.exe 找到 Precompiled Binaries ...
- 数据库中substring的用法 CONVERT(varchar(12) , getdate(), 112 )
Sqlserver中常常要操作一些时间类型的字段转换,我又不太记得住,所以搜集了下面的一些SqlserverConvertDateTime相关的资料发表在自己的小站里,方便自己以后要用的时候寻找,望对 ...
- JAVA中try-catch异常逃逸
有时候一些小的细节,确实比较纠结,对于try-catch-finally代码块中代码依次执行,当try中有exception抛出时,将会有catch拦截并执行,如果没有catch区块,那么except ...