關於Android語言國際化和尺寸適應

來源:互聯網
上載者:User

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

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.