[安卓]应用程序资源(App Resources)

谷歌推荐我们,在开发安卓系统应用程序的时候,要把资源从代码中分离出来,这样便于我们单独维护它们。采取分离的资源设计,我们还可以提供可选资源,支持特定的设备配置譬如不同的语言或屏幕尺寸,随着越来越多的Android设备可选用不同的配置,这变得越来越重要。为了提供不同配置的兼容性,你必须在你的项目的res/目录中组织资源。按类型和配置,使用不同的子目录。

对于任何类型的资源,你可以为你的应用程序提供一个默认和多个可选资源:

  • 默认资源就是不管你什么配置我都强制使用或者没有满足配置的可选资源时使用的资源。
  • 可选资源是提供给特定配置使用的资源。我们给res的自文件夹名提供一个配置限定符,从而把特定配置和特定可选资源关联起来。(【注】所以,这是一种高度分立的资源配置,我们不是在代码中通过if...else等条件语句来判断使用哪个可选资源,而是由系统的某个服务自动判断当前配置和资源文件夹名来加载合适的资源)

1、资源类型:

Directory Resource Type
animator/ XML files that define property animations.
anim/ XML files that define tween animations. (Property animations can also be saved in this directory, but the animator/ directory is preferred for property animations to distinguish between the two types.)
color/ XML files that define a state list of colors. See Color State List Resource
drawable/
Bitmap files (.png, .9.png, .jpg, .gif) or XML files that are compiled into the following drawable resource subtypes:

  • Bitmap files
  • Nine-Patches (re-sizable bitmaps)
  • State lists
  • Shapes
  • Animation drawables
  • Other drawables

See Drawable Resources.

mipmap/ Drawable files for different launcher icon densities. For more information on managing launcher icons with mipmap/ folders, see Managing Projects Overview.
layout/ XML files that define a user interface layout. See Layout Resource.
menu/ XML files that define application menus, such as an Options Menu, Context Menu, or Sub Menu. See Menu Resource.
raw/
Arbitrary files to save in their raw form. To open these resources with a raw InputStream, call Resources.openRawResource() with the resource ID, which is R.raw.filename.

However, if you need access to original file names and file hierarchy, you might consider saving some resources in the assets/ directory (instead of res/raw/). Files in assets/ are not given a resource ID, so you can read them only using AssetManager.

values/
XML files that contain simple values, such as strings, integers, and colors.

Whereas XML resource files in other res/ subdirectories define a single resource based on the XML filename, files in the values/ directory describe multiple resources. For a file in this directory, each child of the <resources> element defines a single resource. For example, a <string> element creates an R.string resource and a <color> element creates an R.color resource.

Because each resource is defined with its own XML element, you can name the file whatever you want and place different resource types in one file. However, for clarity, you might want to place unique resource types in different files. For example, here are some filename conventions for resources you can create in this directory:

See String Resources, Style Resource, and More Resource Types.

xml/ Arbitrary XML files that can be read at runtime by calling Resources.getXML(). Various XML configuration files must be saved here, such as a searchable configuration.

2、资源表示:

3、资源应用:

4、具体资源类型介绍:

时间: 2024-07-28 23:36:59

[安卓]应用程序资源(App Resources)的相关文章

Android应用程序资源的查找过程分析

文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8806798 我们知道,在Android系统中,每一个应用程序一般都会配置很多资源,用来适配不同密度.大小和方向的屏幕,以及适配不同的国家.地区和语言等等.这 些资源是在应用程序运行时自动根据设备的当前配置信息进行适配的.这也就是说,给定一个相同的资源ID,在不同的设备配置之下,查找到的可能是不同的资 源.这个资源查找过程对应用程序来说,是完全透

初识安卓小程序(点击按钮切换屏幕颜色)

如图,点击按钮就会切换屏幕的颜色 首先,先创建一个安卓项目(我的版本是4.4.2的),名字为"world",当然,也可以别的名称 然后在res文件夹下找到layout文件夹,找到activity_main.xml或fragment_main.xml,在里面输入或拖拽按钮 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="htt

Android官方文档之App Resources(中)

本文将继续介绍App Resources中的资源类型(Animation.Color State List.String.Style). 如果需要了解Android中的资源规范,您可以访问我翻译的这篇官方文档:<Android官方文档之App Resources(上)>. 如需访问官方原文,您可以点击这个链接:<Resource Types>. 在下一篇文章中(Android官方文档之App Resources(下)),将介绍App Resources中其余的资源类型(Layout

Android官方文档之App Resources(下)

本文将介绍Android中Resource Types的drawable.menu.layout.如需访问官方原文,您可以点击这些链接: <Drawable Resources> <Layout Resource> <Menu Resource> 如需了解Android中的其他资源介绍(Animation.Color State List.String.Style),您可以点击这个链接:<Android官方文档之App Resources(中)>. 布局资源

Android官方文档之App Resources(上)

想做出一款出色的App,仅仅编写Java代码还不够.在代码中调用资源(Resources ),如位图(bitmaps).布局资源( layout definitions).UI中需要展示的字符串资源(user interface strings).动画资源(animation instructions)等,可以让您的App更加出色. 本文将介绍Android中各种类型的资源.以及获取资源的方式,如需访问官方原文,您可以点击这个链接:<App Resources>. 可为App提供的资源(Pro

Android应用程序资源的编译和打包过程分析

文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/8744683 我们知道,在一个APK文件中,除了有代码文件之外,还有很多资源文件.这些资源文件是通过Android资源打包工具aapt(Android Asset Package Tool)打包到APK文件里面的.在打包之前,大部分文本格式的XML资源文件还会被编译成二进制格式的XML资源文件.在本文中,我们就详细分析 XML资源文件的编译和打包

初识安卓小程序(Android短信发送器)

首先,先创建一个安卓项目(我的版本是4.4.2的),名字为"短信发送器" 然后在res文件夹下找到layout文件夹,找到activity_main.xml或fragment_main.xml,在里面输入或拖拽按钮 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tool

初识安卓小程序(Android电话拨号器)

首先,先创建一个安卓项目(我的版本是4.4.2的),名字为"电话拨号器",创建的时候点击"clipart",如图: 然后在res文件夹下找到layout文件夹,找到activity_main.xml或fragment_main.xml,在里面输入或拖拽按钮 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="

初识安卓小程序(开关灯——单选多选按钮控制)

如图: 点击单选按钮"开灯",多选按钮就会显示"关灯"且方块里有对勾:反之,点多选按钮,单选按钮也自动改变. 首先,先创建一个安卓项目(我的版本是4.4.2的),名字为"bulb",把两张图片:开灯与关灯状态的图片放入"drawable-"随意一个文件夹下 然后在res文件夹下找到layout文件夹,找到activity_main.xml或fragment_main.xml,在里面输入或拖拽按钮 <RelativeLay