关于Android语言国际化和尺寸适应

Providing Resources

You should always externalize application resources such as images and strings from your code, so that you can maintain them independently. You should also provide alternative resources for specific device configurations, by grouping them in specially-named resource directories. At runtime, Android uses uses the appropriate resource based on the current configuration. For example, you might want to provide a different UI layout depending on the screen size or different strings depending on the language setting.

Once you externalize your application resources, you can access them using resource IDs that are generated in your project'sR class. How to use resources in your application is discussed in Accessing Resources. This document shows you how to group your resources in your Android project and provide alternative resources for specific device configurations.

      

       当我们为APP提供资源,比如图片或者字符串的时候,我们可以把他们独立出来,我们可以提供不同的资源为特殊的设备配置,把他们放到特殊命名的res 目录下。在运行时,android将会根据当前的配置调用适当的res。比如说,你也许想要根据不同的屏幕大小提供不同的UI,或者根据语言提供不同的字符串。

       当你我们调用这些资源的时候可以,通过通过R文件里生成ID,如果想了解如何使用RES,可以通过学习Accessing Resources这篇文章。本文,将讲述android如何为不同的设备,准备资源。

 

Grouping Resource Types

You should place each type of resource in a specific subdirectory of your project'sres/ directory. For example, here's the file hierarchy for a simple project:

我们应该把不同类型的资源放到res的不同的子目录下,下图就是一个简单的res目录结构。

 

 

 工程res/下面可以支持的资源目录:

目录

资源类型

animator/

定义动画属性(property animations)的XML文件.

anim/

定义补间动画(tween animations)的XML文件. (动画的属性也可以保存在这个目录下,但是animator/目录是用来保存动画属性的并区别开两个目录)

color/

定义了一些colors对应值的XML文件,见Color State List Resource

drawable/

Bitmap文件(.png,.9.png,.jpg,.gif)或者能被编译为drawable类型的XML文件:

·         Bitmap files

·         Nine-Patches(re-sizable bitmaps)State lists

·         Color drawables

·         Shapes

·         Animation drawables

见 Drawable Resources

layout/

定义用户界面布局的XML文件. 见 Layout Resource.

menu/

定义程序menus的XML文件,比如Options Menu,Context Menu,或者Sub Menu.见Menu Resource

raw/

放置任意的文件,必须为原始形式。使用raw InputStream来打开这些资源文件,调用Resources.openRawResource()方法来打开resource ID对应的资源,比如R.raw.filename。

然而,如果你需要访问原始文件名字和文件目录结构,你可以考虑将一些资源文件保存在 assets/ 目录下(而不是res/raw/)。放在assets/下的文件不提供一个资源ID,所以你只能使用 AssetManager来读取它们。

values/

包含一些简单值的XML文件,比如strings,integers,和颜色。

在其他res/子目录下定义的XML资源文件定义的是一个单独的基于XML文件名的资源,在values/目录下的文件描述了多种资源。例如这个目录下的某个资源文件,每个<resources>的子节点定义了一个单独资源。比如,<string>节点创建了一个R.string资源,<color>节点创建了一个R.color资源。

因为每个资源都是使用自己的XML文件定义的,你可以任意命名文件,并在一个文件里面放不同种类的资源。但是,为了清晰可见(for clarity),你也许想要在一个文件里面放置统一的资源类型。例如,下面是这个目录下创建资源文件的俗称约定:

·         arrays.xml:放置资源数组(typed arrays)

·         colors.xml:放置颜色值(color values)

·         dimens.xml:放置长度值(dimension values)

·         strings.xml:放置字符串值(string values)

·         styles.xml:放置样式(styles)

见 String Resource,Style Resource以及 More Resource Types。

xml/

在运行时可以通过调用Resources.getXML()方法来读取该目录下的任意XML文件。各种XML配置文件都必须保存在这里,比如searchable configuration

 

 

  • ldpi: Low-density screens; approximately 120dpi.
  • mdpi: Medium-density (on traditional HVGA) screens; approximately 160dpi.
  • hdpi: High-density screens; approximately 240dpi.
  • xhdpi: Extra high-density screens; approximately 320dpi.Added in API Level 8

     

  • You can specify multiple qualifiers for a single set of resources, separated by dashes. For example,drawable-en-rUS-land applies to US-English devices in landscape orientation.

     

  • The qualifiers must be in the order listed in table 2. For example:     
    • Wrong: drawable-hdpi-port/
    • Correct: drawable-port-hdpi/
  • Alternative resource directories cannot be nested. For example, you cannot haveres/drawable/drawable-en/.

     

  • Values are case-insensitive.  The resource compiler converts directory names    to lower case before processing to avoid problems on case-insensitive    file systems. Any capitalization in the names is only to benefit readability.

     

  • Only one value for each qualifier type is supported. For example, if you want to use the same drawable files for Spain and

     

    France, you cannot have a directory nameddrawable-rES-rFR/. Instead you need two resource directories, such asdrawable-rES/ and drawable-rFR/, which contain the appropriate files. However, you are not required to actually duplicate the same files in both locations. Instead, you can create an alias to a resource. See Creating alias resources below.

     

     如果有两种资源,则放到两个不同的drawable目录下,不要写到一起。

     

    如果希望不重复定义相同的文件,可以把两种资源关联到一起。例如

     

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    
        <color name="yellow">#f00</color>
        <color name="highlight">@color/yellow</color>
    
    </resources>


     

     

     

     

     

     

     

     

     

     

    翻译节选自:docs\guide\topics\resources\providing-resources.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值