Application Fundamentals(6)

App Resources

An Android app is composed of more than just code—it requires resources that are separate from the source code, such as images, audio files, and anything relating to the visual presentation of the app.
一个安卓应用不仅仅只由代码组成,它更需要独立于代码的资源,比如图像,音频文件,以及任何与该应用的视觉显示有关的东西。

For example, you should define animations, menus, styles, colors, and the layout of activity user interfaces with XML files.
比如,你应该通过XML文件定义动画,菜单,样式,颜色以及活动用户界面的布局。

Using app resources makes it easy to update various characteristics of your app without modifying code and—by providing sets of alternative resources—enables you to optimize your app for a variety of device configurations (such as different languages and screen sizes).
使用应用资源会使得更新应用各种各样的特性而不需要去修改代码更加简单,通过提供可替代资源集,使得你的应用能在各种各样的设备配置下达到最优化。(比如不同的语言和屏幕尺寸)

For every resource that you include in your Android project, the SDK build tools define a unique integer ID, which you can use to reference the resource from your app code or from other resources defined in XML.
对于你的安卓工程中包含的每一个资源,软件开发构建工具会为它们定义独一无二的整数ID,你可以通过这些id在你的应用代码中或者其他定义在XML文件的资源来引用资源。

For example, if your app contains an image file named logo.png (saved in the res/drawable/ directory), the SDK tools generate a resource ID named R.drawable.logo, which you can use to reference the image and insert it in your user interface.
比如,如果你的应用包含一个文件名为logo.png的图片(保存在res/drawable/ 目录),SDK工具就会生成一个叫做R.drawable.logo的资源ID,你可以使用这个ID来引用这张图片并将其插入你的用户界面。

One of the most important aspects of providing resources separate from your source code is the ability for you to provide alternative resources for different device configurations.
提供与源代码分离的资源最重要的一方面就是,对于不同的设备配置能够提供可替代资源的能力。

For example, by defining UI strings in XML, you can translate the strings into other languages and save those strings in separate files.
比如,通过将UI字符串定义在XML文件中,你可以将字符串转换为其他语言,并且将这些字符串保存在相互隔离的文件中。

Then, based on a language qualifier that you append to the resource directory’s name (such as res/values-fr/ for French string values) and the user’s language setting, the Android system applies the appropriate language strings to your UI.
然后,基于你附加在资源目录名的的语言限定符
(比如res/values-fr/就是用于法国的字符串值的)
和用户的语言设置,安卓系统会使用合适的语言字符串到你的界面上。

Android supports many different qualifiers for your alternative resources.
安卓为你的可替代资源提供了非常多种的限定符。

The qualifier is a short string that you include in the name of your resource directories in order to define the device configuration for which those resources should be used.
限定符是一个包含在你的资源目录名中的短字符串,是为了定义应该使用这些资源的设备配置。

As another example, you should often create different layouts for your activities, depending on the device’s screen orientation and size.
在另一个例子中,你可能经常根据不同的设备屏幕朝向和尺寸,为你的activities创建不同的布局文件。

For example, when the device screen is in portrait orientation (tall), you might want a layout with buttons to be vertical, but when the screen is in landscape orientation (wide), the buttons should be aligned horizontally.
比如,当设备屏幕是纵向的(高屏),你可能需要一个按钮都是垂直排列的布局,但是当屏幕是横向的时候(宽屏),按钮就应该是水平对齐的。

To change the layout depending on the orientation, you can define two different layouts and apply the appropriate qualifier to each layout’s directory name. Then, the system automatically applies the appropriate layout depending on the current device orientation.
为了能够根据方向改变布局,你可以定义两种不同的布局,并且将适当的限定符应用到每个布局的目录名中。然后系统会根据当前设备朝向自动使用适当的布局。

For more about the different kinds of resources you can include in your application and how to create alternative resources for different device configurations, read Providing Resources.
关于您可以在应用程序中包含的不同种类的资源,以及如何为不同的设备配置创建替代资源,请阅读 Providing Resources。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值