你應該將代碼和資源文件(好比圖片和字符串)分開,這樣你能夠單獨的來維護這些資源文件。你也能夠經過特殊的名字的文件路徑,爲特定的設備配置提供可替換的資源文件。Android會基於如今的配置自動應用合適的資源文件。例如,你能夠根據不一樣的屏幕尺寸提供不一樣的UI佈局。 一旦你將資源文件與程序代碼分開保存,你能夠經過資源IDs來使用對應的資源文件。這些資源ID在項目的R類生成。關於如何在你的程序裏面使用資源,能夠看看 Accessing Resources Grouping Resource Types/資源文件的分類 在工程的res/目錄下,每一個子目錄對應一種類型的資源文件。例如,下面是一個簡單工程的文件樹狀結構。 MyProject/ src/ MyActivity.java res/ drawable/ icon.png layout/ main.xml info.xml values/ strings.xml res/目錄下包含了全部的資源文件(在子目錄下):圖片資源類型,兩種佈局資源文件,和一個string資源文件。這個資源目錄的名字是很是重要的,如Table1 Table1. 工程res/下面能夠支持的資源目錄:目錄 資源類型 anim/ 定義動畫tween的XML文件,見 Animation Resources 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 menu/ 定義程序menus的XML文件,好比Options Menu, Context Menu, 或者Sub Menu.見Menu Resource raw/ 放置任意的文件,必須爲原始形式(Arbitrary files to save in their)。這個文件夾的文件不能是被壓縮過的(Files in here are not compressed)。使用raw InputStream來打開這些資源文件,調用Resources.openRawResource()方法來打開resource ID對應的資源,好比R.raw.filename。 然而,若是你須要訪問原始文件名字和文件目錄結構,你能夠考慮將一些資源文件保存在 assets/ 目錄下(代替res/raw/)。放在assets/下的文件不須要給一個資源ID,因此你可使用 AssetManager來讀取它們。 values/ 包含一些簡單值的XML文件,好比strings,integers,和顏色 在其餘res/子目錄下定義的XML資源文件定義的是一個單獨的基於XML文件名的資源(Whereas XML resource files in other res/ subdirectories define a single resource based on the XML filename),在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/ 該文件下的xml能夠在運行時調用Resources.getXML()來讀取。各類XML配置文件都必須保存在這裏,好比searchable configuration 注意:你不能夠直接將資源文件保存在res/目錄下。 Resource Types 文檔有更多資源類型介紹。 Accessing Resource文件介紹瞭如何用代碼訪問 res/子目錄下的資源文件。 Providing Alternative Resources/提供可替換的資源 幾乎每一個程序都應該給特定的設備配置提供可替換的資源。例如,能夠爲不一樣的屏幕尺寸提供可替換的資源,不一樣的語言提供可替換的字符串。運行時,Android會自動檢查當前屏幕配置並加載合適的資源。 要指定一組資源配置的具體方案(To specify configuration-specific alternatives for a set of resources): 在res/下建立一個新的目錄 <resources_name>-<config_qualifier> <resources_name>是和默認資源相對應的目錄名稱 <config_qualifier>是指定一個配置相應的名稱,同時放在這個目錄下的資源會在這個配置上使用。你能夠附加多個<config_qualifier>。用破折號進行分開。 在你新的目錄下保存你的可替換資源,這個資源文件必須和默認的資源命名同樣。 例如,下面是一些默認資源和可替換資源: res/ drawable/ icon.png background.png drawable-hdpi/ icon.png background.png 這個hdpi意思是說在這個目錄下的資源將會在高分辨率(hign-density)的設備上使用。同時每一個drawable目錄下的圖片根據特定屏幕密度繪製大小,文件名是相同的。這樣,你使用引用icon.png和backgrou.png的資源ID是相同的,可是android會根據當前設備配置選擇最合適的drawable。 Android支持多種配置標識符(qualifiers),你能夠給你一個目錄名字增長多個標識符(qualifiers),經過破折號分開。Table 2列出了合法的配置標識符(qualifiers),若是你使用了多個標識符,則按順序來決定優先權,資源必須防止在下表裏出來的並以此命名的文件夾下面: Table 2 . 可替換資源標識符名字Qualifier Values Description MCC and MNC Examples: mcc310 mcc310-mnc004 mcc208-mnc00 etc. MCC和MNC分別是從設備的SIM卡讀取出來的mobile country code(MCC),實際上是可選的mobile network code(MNC)。例如,mcc310是U.S on any carrier, mcc310-mnc004 is U.S on Verizon, and mcc208-mnc00 is France on Orange. 若是設備使用無線鏈接(radio connection)(GSM手機),MCC從SIM讀取,同時MNC從設備所鏈接的網絡中讀取。 你也能夠單獨的使用MCC(例如,在應用程序中包含特定國家的法律資源)。若是你僅僅須要指定語言,那麼使用 language and region 標識符代替(下面會討論)。若是你決定使用MCC和MNC標識符,你應該仔細測試它是否能夠正常運行。 也能夠看看配置域 mcc 和 mnc,分別指示當前移動國家代碼和移動網絡代碼。 Language and region Examples: en fr en-rUS fr-rFR fr-rCA etc. 語言被定義爲ISO 639-1 代碼的兩個字母,能夠選擇 ISO 3166-1-alpha-2區域(小寫的「r」開頭)的兩個字母。 該代碼不區分大小寫,r前綴用於區分該地區的某個地方,你不能單獨的指定一個區域。 若是用戶改變了他系統設置裏面語言,在你程序運行時就改變。能夠看看 Handling Runtime Changes裏面關於如何在程序運行時發生變化的文檔。(This can change during the life of your application if the user changes his or her language in the system settings. See Handling Runtime Changes for information about how this can affect your application during runtime.) Localization完整的介紹瞭如何根據其餘語音本土化你的程序。 Screen size small normal large small: 基於低密度QVGA屏幕可用空間的屏幕(Screens based on the space available on a low-density QVGA screen)。Considering a portrait HVGA display, this has the same available width but less height—it is 3:4 vs. HVGA’s 2:3 aspect ratio. Examples are QVGA low density and VGA high density. normal:Screen based on the traditional medium-density HVGA screen. A screen is considered to be normal if it is at least this size(independent of density) and not larger. Examples of such screens a WQVGA low density, HVGA medium density, WVGA high density. large: Screens based on the space available on a medium-density VGA screen. Such a screen has significantly more available space in both width and height than an HVGA display. Examples are VGA and WVGA medium density screens. See Supporting Multiple Screens for more information. Also see the screenLayout configuration field, which indicates whether the screen is small, normal, or large. Wider/taller screens long notlong long:長屏幕,好比 WQVGA,WVGA,FWVGA notlong:不是長屏幕,好比QVGA,HVGA,和VGA 這個純粹基於屏幕的長寬比(aspect ratio)(一個長屏幕是寬的)。且它與屏幕的方向是不相關的。 也能夠看看screenLayout配置域,標明瞭屏幕是不是long的。 Screen orientation port land port:設備處於縱向(垂直)。Device is in portrait orientation(vertical) land:設備處於橫向(水平)。Device is in landscape orientation(horizontal) 若是用戶在屏幕旋轉,同時你的程序在運行,發生改變(This can change during the life of your application if the user rotates the screen.)。Handling Runtime Changes 介紹了是如何在運行時影響你的程序的。 也能夠看看 orientation 配置域,標明瞭當前設備的方向。 Dock mode car desk car:Device is in a car dock desk:Device is in a desk dock Added in API Level 8 This can change during the life of your application if the user places the device in a dock. You can enable or disable this mode using UiModeManager. See Handling Runtime Changes for information about how this affects your application during runtime. Night mode night notnight night: Night time notnight: Day time Add in API Level 8 This can change during the life of your application if night mode is left in auto mode(default), in which case the mode changes based on the time of day. You can enable or disable this mode using UiModeManager. See Handling Runtime Changes for infor Screen pixel density(dpi) ldpi mdpi hdpi nodpi ldpi:低分辨率的屏幕,大約 120dpi。Low-density screens; approximately 120dpi. mdpi:中等分辨率的屏幕,大約160dpi。Medium-density(on traditional HVGA) screens; approximately 160 dpi. hdpi:高分辨率的屏幕,大約240dpi。High-density screens; approximately 240 dpi. nodpi:這能夠那些你不想爲了匹配屏幕分辨率而進行拉伸的位圖資源。This can be used for bitmap resources that you do not want to be scaled to match the device density. Touchscreen type notouch stylus finger notouch:設備沒有觸摸屏。Device does not have a touchscreen stylus:設備爲電阻觸屏,適合手寫筆。Device has a resistive touchscreen that’s suited for use with a stylus. finger:設備爲觸摸屏。 能夠看看 touchscreen,標明瞭設備的觸摸屏類型。 Keyboard availability keysexposed keyssoft keysexposed:設備有一個可用的鍵盤。若是該設備有一個軟件鍵盤啓用(這是頗有可能),這可能使用的硬件鍵盤,即便不暴露給用戶,即便該設備沒有硬件鍵盤。若是沒有提供軟件鍵盤,或者不可用,那麼僅當硬件鍵盤被暴露時使用。(Device has a keyboard available. If the device has a software keyboard enabled (which is likely), this may be used even when the hardware keyboard is not exposed to the user, even if the device has no hardware keyboard. If no software keyboard is provided or it’s disabled, then this is only used when a hardware keyboard is exposed.) keyshidden:設備有一個可用的硬件鍵盤,可是是被隱藏的。同時設備沒有可用的軟件鍵盤。(Device has a hardware keyboard available but it is hidden and the device does not have a software keyboard enabled). keyssoft:設備有一個可用的軟件鍵盤,不管它是否是可用的狀態。 若是你提供了keysexposed 資源,可是沒有keyssoft資源,系統會使用keysexposed資源而忽視鍵盤是否是可見,只要系統有一個可用的軟件鍵盤(If you provide keysexposed resources, but not keyssoft resources, the system uses the keysexposed resources regardless of whether a keyboard is visible, as long as the system has a software keyboard enabled)。 若是用戶打開了一個硬件鍵盤的時候發生改變(This can change during the life of your application)。See Handling Runtime Changes for information about how this affects your application during runtime. 也能夠看看hardKeyboardHidden和keyboardHidden,分別標明瞭一個硬件鍵盤的能見度和其餘鍵盤的能見度。(Also see the configuration fields hardKeyboardHidden and keyboardHidden, which indicate the visibility of a hardware keyboard and the visibility of any kind of keyboard(including software), respectively) Primary text input method nokeys qwerty 12key nokeys:Device has no hardware keys for text input qwert:Device has a hardware qwerty keyboard, whether it’s visible to the user or not 12key. 12key:Device has a hardware 12-key keyboard, whether it’s visible to the user or not. Navigation key availability navexposed navhidden navexposed: Navigation keys are available to the user navhidden: Navigation keys are not available(such as behind a closed lid) This can change during the life of your application if the user reveals the navigation keys. See Handling Runtime Changes for information about how this affects your application during runtime. Also see the navigationHidden configuration field, which indicates whether navigation keys are hidden. Primary non-touch navigation method nonav dpad trackball wheel nonav:Device has no navigation facility other than using the touchscreen. dpad:Device has a directional-pad (d-pad) for navigation. trackball:Device has a trackball for navigation. wheel: Device has a directional wheel(s) for navigation(uncommon). Alson see the navigation configuration field, which indicates the type of navigation method available. API Level Examples: v4 v5 v6 v7 etc The API Level supported by the device, for example v1 for API Level 1(Android 1.0) or v5 for API Level 5(Android 2.0). See the Android API Levels document for more information about these values. 標識符名稱的規則 下面是使用資源標識符名稱的一些規則: 你能夠爲一組類型的資源指定多個標識符,並用破折號進行分開。例如,drawable-en-rUS-land的資源將用於US-English而且此時爲橫屏的設備上。 標識符的順序必須與table 2相同,例如: Wrong:drawable-hdpi-port/ Correct:drawable-port-hdpi/ 可替換資源目錄不能嵌套。例如,你不能有 res/drawable/drawable-en/ Values對大小寫不敏感,資源編譯器會在處理前將目錄名轉換爲小寫,以免不區分大小寫的文件系統出現問題。Any capitalization in the names is only to benefit readability. (未完成) 原創文章,轉載請註明: 轉載自Girl is coding 本文連接地址: Providing Resources/Android可以使用的資源文件