1# Resource Categories and Access 2 3During application development, you may need to use different resources, such as colors, fonts, spacing, and images, based on the device or configuration. Depending on the resource type, you can achieve this using the following methods: 4 5- Application resources: Configure device- or configuration-specific resources in the resource files. 6 7- System resources: Use the preset resource definitions (that is, [layered parameters](../../design/ux-design/design-resources.md), with which a resource with the same ID has different values under different configurations, including device types and color modes). 8 9## Resource Categories 10 11Resource files used during application development must be stored in specified directories for management. There are two types of resource directories, namely, resource directories and resource group directories. The resource directories are the **base**, qualifiers, **rawfile**, and **resfile** directories. The resource group directories are the **element**, **media**, and **profile** directories. 12 13> **NOTE** 14> 15> The common resource files used across projects in the stage model are stored in the **resources** directory under **AppScope**. 16 17Example of the **resources** directory: 18``` 19resources 20|---base 21| |---element 22| | |---string.json 23| |---media 24| | |---icon.png 25| |---profile 26| | |---test_profile.json 27|---en_US // Default directory. When the device language is en-us, resources in this directory are preferentially matched. 28| |---element 29| | |---string.json 30| |---media 31| | |---icon.png 32| |---profile 33| | |---test_profile.json 34|---zh_CN // Default directory. When the device language is zh-cn, resources in this directory are preferentially matched. 35| |---element 36| | |---string.json 37| |---media 38| | |---icon.png 39| |---profile 40| | |---test_profile.json 41|---en_GB-vertical-car-mdpi // Example of a qualifiers directory, which needs to be created on your own. 42| |---element 43| | |---string.json 44| |---media 45| | |---icon.png 46| |---profile 47| | |---test_profile.json 48|---rawfile // Other types of files are saved as raw files and will not be integrated into the resources.index file. You can customize the file name as needed. 49|---resfile // Other types of files are saved as raw files and will not be integrated into the resources.index file. You can customize the file name as needed. 50``` 51### Resource Directories 52 53#### base Directory 54 55The **base** directory is provided by default. Under this directory, the **element** subdirectory stores basic elements such as strings, colors, and boolean values, and the **media** and **profile** subdirectories store resource files such as media, animations, and layouts.<br> 56Resource files in the subdirectories are compiled into binary files, and each resource file is assigned an ID. Resource files in the subdirectory are referenced based on the resource type and resource name. 57 58#### Qualifiers Directory 59 60**en_US** and **zh_CN** are two default qualifiers directories. You need to create other qualifiers directories on your own. Under this directory, the subdirectories store basic elements such as strings, colors, and boolean values, as well as resource files such as media, animations, and layouts.<br>Resource files in the subdirectories are compiled into binary files, and each resource file is assigned an ID. Resource files in the subdirectories are referenced based on the resource type and resource name. 61 62**Naming Conventions for Qualifiers Directories** 63 64The name of a qualifiers directory consists of one or more qualifiers that represent the application scenarios or device characteristics, covering the mobile country code (MCC), mobile network code (MNC), language, script, country or region, screen orientation, device type, night mode, and screen density. The qualifiers are separated using underscores (\_) or hyphens (\-). Before creating a qualifiers directory, familiarize yourself with the directory naming conventions. 65 66- Qualifiers are ordered in the following sequence: **\_MCC_MNC-language_script_country/region-orientation-device-color mode-density_**. You can select one or multiple qualifiers to name your subdirectory based on your application scenarios and device characteristics. 67 68- Separation between qualifiers: The language, script, and country/region qualifiers are separated using underscores (\_); the MNC and MCC qualifiers are also separated using underscores (\_); other qualifiers are separated using hyphens (\-). For example, **zh_Hant_CN** and **zh_CN-car-ldpi**. 69 70- Value range of qualifiers: The value of each qualifier must meet the requirements specified in the following table. Otherwise, the resource files in the resources directory cannot be matched. 71 72Table 2 Requirements for qualifier values 73 74| Qualifier Type | Description and Value Range | 75| ----------- | ---------------------------------------- | 76| MCC&MNC| Indicates the MCC and MNC, which are obtained from the network where the device is registered.<br>The MCC can be either followed by the MNC with an underscore (_) in between or be used independently. For example, **mcc460** represents China, and **mcc460_mnc00** represents China Mobile.<br>For details about the value range, refer to **ITU-T E.212** (the international identification plan for public networks and subscriptions).| 77| Language | Indicates the language used by the device. The value consists of two or three lowercase letters. For example, **zh** indicates Chinese, **en** indicates English, and **mai** indicates Maithili.<br>For details about the value range, refer to **ISO 639** (codes for the representation of names of languages).| 78| Text | Indicates the script type used by the device. The value starts with one uppercase letter followed by three lowercase letters. For example, **Hans** indicates simplified Chinese, and **Hant** indicates traditional Chinese.<br>For details about the value range, refer to **ISO 15924** (codes for the representation of names of scripts).| 79| Country/Region | Indicates the country or region where the user is located. The value consists of two or three uppercase letters or three digits. For example, **CN** indicates China, and **GB** indicates the United Kingdom.<br>For details about the value range, refer to **ISO 3166-1** (codes for the representation of names of countries and their subdivisions).| 80| Screen orientation | Indicates the screen orientation of the device. The value can be:<br>- **vertical**: portrait orientation<br>- **horizontal**: landscape orientation| 81| Device type | Indicates the device type. The value can be:<br>- **car**: head unit<br>- **tablet**: tablet<br>- **tv**: smart TV<br>- **wearable**: smart wearable| 82| Color mode | Indicates the color mode of the device. The value can be:<br>- **dark**: dark mode<br>- **light**: light mode| 83| Screen density | Indicates the screen density of the device, in dpi. The value can be:<br>- **sdpi**: screen density with small-scale dots per inch (SDPI). This value is applicable for devices with a DPI range of (0, 120].<br>- **mdpi**: medium-scale screen density (Medium-scale Dots Per Inch), applicable to DPI whose value is (120, 160] device.<br>- **ldpi**: screen density with large-scale dots per inch (LDPI). This value is applicable for devices with a DPI range of (160, 240].<br>- **xldpi**: screen density with extra-large-scale dots per inch (XLDPI). This value is applicable for devices with a DPI range of (240, 320].<br>- **xxldpi**: screen density with extra-extra-large-scale dots per inch (XXLDPI). This value is applicable for devices with a DPI range of (320, 480].<br>- **xxxldpi**: screen density with extra-extra-extra-large-scale dots per inch (XXXLDPI). This value is applicable for devices with a DPI range of (480, 640].| 84 85#### rawfile Directory 86 87You can create multiple levels of subdirectories with custom names to store various resource files.<br>Resource files in the subdirectories are directly packed into the application without being compiled, and no IDs will be assigned to the resource files. The subdirectories are referenced based on the specified file path and file name. 88 89#### resfile Directory 90 91You can create multiple levels of subdirectories with custom names to store various resource files.<br>Resource files in the subdirectories are directly packed into the application without being compiled, and no IDs will be assigned to the resource files. After an application is installed, the **resfile** directory is decompressed to the application sandbox path. You can obtain the path through the [resourceDir](../reference/apis-ability-kit/js-apis-inner-application-context.md#attributes) attribute of **Context**. 92 93### Resource Group Directories 94 95Resource group directories include **element**, **media**, and **profile**, which are used to store resource files of specific types. 96 97 **Table 3** Resource group directories 98 99| Directory | Description | Resource File | 100| --------- | ---------------------------------------- | ---------------------------------------- | 101| element | Element resources. Each type of data is represented by a JSON file. (Only files are supported in this directory.) The options are as follows:<br>- **boolean**: boolean data<br>- **color**: color data<br>- **float**: floating-point data<br>- **intarray**: array of integers<br>- **integer**: integer data<br>- **pattern**: pattern data<br>- **plural**: plural form data<br>- **strarray**: array of strings<br>- **string**: string data| It is recommended that files in the **element** subdirectory be named the same as the following files, each of which can contain only data of the same type:<br>- boolean.json<br>- color.json<br>- float.json<br>- intarray.json<br>- integer.json<br>- pattern.json<br>- plural.json<br>- strarray.json<br>- string.json | 102| media | Indicates media resources, including non-text files such as images, audios, and videos. (Only files are supported in this directory.)<br>Table 4 and Table 5 describe the types of images, audios, and videos. | The file name can be customized, for example, **icon.png**. | 103| profile | Indicates a custom configuration file. You can obtain the file content by using the [getProfileByAbility](../reference/apis-ability-kit/js-apis-bundleManager.md#bundlemanagergetprofilebyability) API. (Only files are supported in this directory.) | The file name can be customized, for example, **test_profile.json**. | 104 105**Media Resource Types** 106 107Table 4 Image resource types 108 109| Format | File Name Extension| 110| ---- | ----- | 111| JPEG | .jpg | 112| PNG | .png | 113| GIF | .gif | 114| SVG | .svg | 115| WEBP | .webp | 116| BMP | .bmp | 117 118Table 5 Audio and video resource types 119 120| Format | File Name Extension | 121| ------------------------------------ | --------------- | 122| H.264 AVC |.3gp | 123| Baseline Profile (BP) | .mp4 | 124 125**Resource File Examples** 126 127The content of the **color.json** file is as follows: 128 129 130```json 131{ 132 "color": [ 133 { 134 "name": "color_hello", 135 "value": "#ffff0000" 136 }, 137 { 138 "name": "color_world", 139 "value": "#ff0000ff" 140 } 141 ] 142} 143``` 144 145The content of the **float.json** file is as follows: 146 147 148```json 149{ 150 "float":[ 151 { 152 "name":"font_hello", 153 "value":"28.0fp" 154 }, 155 { 156 "name":"font_world", 157 "value":"20.0fp" 158 } 159 ] 160} 161``` 162 163The content of the **string.json** file is as follows: 164 165 166```json 167{ 168 "string":[ 169 { 170 "name":"string_hello", 171 "value":"Hello" 172 }, 173 { 174 "name":"string_world", 175 "value":"World" 176 }, 177 { 178 "name":"message_arrive", 179 "value":"We will arrive at %s." 180 } 181 ] 182} 183``` 184 185The content of the **plural.json** file is as follows: 186 187 188```json 189{ 190 "plural":[ 191 { 192 "name":"eat_apple", 193 "value":[ 194 { 195 "quantity":"one", 196 "value":"%d apple" 197 }, 198 { 199 "quantity":"other", 200 "value":"%d apples" 201 } 202 ] 203 } 204 ] 205} 206``` 207 208## Creating a Resource Directory and Resource File 209 210You can create a directory and its files under the **resources** directory based on the preceding descriptions of the qualifiers directories and resource group directories. DevEco Studio provides a wizard for you to create resource directories and resource files. 211 212### Creating a Resource Directory and Resource File 213 214Right-click the **resources** directory and choose **New** > **Resource File**. If no qualifier is selected, the file is created in a resource group directory under **base**. If one or more qualifiers are selected, the system automatically generates a subdirectory and creates the file in this subdirectory. 215 216In **File name**, enter the name of the resource file to create. In **Resource type**, select the type of the resource group, which is **element** by default. In **Root Element**, select a resource type. To select a qualifier, highlight it under **Available qualifiers** and click the right arrow. To deselect a qualifier, highlight it under **Selected qualifiers** and click the left arrow.<br>The created directory is automatically named in the format of *Qualifiers.Resource group type*. For example, if you create a subdirectory by setting **Color Mode** to **Dark** and **Resource type** to **Element**, the system automatically generates a subdirectory named **dark.element**. 217 218  219 220### Creating a Resource Directory 221 222Right-click the **resources** directory and choose **New** > **Resource Directory** to create a directory only. By default, the **base** directory is created. You can create qualifiers directories as required, by specifying the qualifier and resource group type. 223 224  225 226### Creating a Resource File 227 228Right-click a directory under **resources** and choose **New** > **XXX Resource File**. This operation creates a resource file under this directory. For example, you can create an element resource file in the **element** subdirectory. 229 230  231 232## Resource Access 233 234### Application Resources 235 236- To reference an application resource in a project, use the "$r('app.type.name')" format. **app** indicates the resource defined in the **resources** directory of the application. **type** indicates the resource type (or the location where the resource is stored). The value can be **color**, **float**, **string**, **plural**, or **media**. **name** indicates the resource name, which you set when defining the resource. 237 238- When referencing resources in the **rawfile** subdirectory, use the "$rawfile('filename')" format. Wherein **filename** indicates the relative path of a file in the rawfile subdirectory, which must contain the file name extension and cannot start with a slash (/). 239 240- To obtain the descriptor of a file in the **rawfile** subdirectory, you can use the [getRawFd](../reference/apis-localization-kit/js-apis-resource-manager.md#getrawfd9) API, whose return value **descriptor.fd** is the file descriptor (FD). To access the file with this FD, use {fd, offset, length}. 241 242- The **resfile** directory provides only resource paths. To operate the resources, go to the respective paths. 243 244> **NOTE** 245> 246> Resource descriptors accept only strings, such as **'app.type.name'**, and cannot be combined. 247> 248> The return value of **$r** is a **Resource** object. You can obtain the corresponding string by using the [getStringValue](../reference/apis-localization-kit/js-apis-resource-manager.md#getstringvalue9) API. 249 250As described in [Resource Group Directories](#resource-group-directories), you can reference .json resource files, including **color.json**, **string.json**, and** plural.json**.<br>The usage is as follows: 251 252```ts 253Text($r('app.string.string_hello')) 254 .fontColor($r('app.color.color_hello')) 255 .fontSize($r('app.float.font_hello')) 256 257Text($r('app.string.string_world')) 258 .fontColor($r('app.color.color_world')) 259 .fontSize($r('app.float.font_world')) 260 261// Reference string resources. The first parameter of $r indicates the string resource, and the second parameter is used to replace %s in the string.json file. 262// In this example, the resultant value is "We will arrive at five of the clock". 263Text($r('app.string.message_arrive', "five of the clock")) 264 .fontColor($r('app.color.color_hello')) 265 .fontSize($r('app.float.font_hello')) 266 267// Reference plural resources. The first parameter of $r indicates the plural resource, the second parameter indicates the number of plural resources (for English, one indicates singular and is represented by 1, and other indicates plural and is represented by an integer greater than or equal to 1; for Chinese, other indicates both singular and plural), and the third parameter is used to replace %d. 268// In this example, the resultant value is "5 apples". 269Text($r('app.plural.eat_apple', 5, 5)) 270 .fontColor($r('app.color.color_world')) 271 .fontSize($r('app.float.font_world')) 272 273Image($r('app.media.my_background_image')) // Reference media resources. 274 275Image($rawfile('test.png')) // Reference an image in the rawfile directory. 276 277Image($rawfile('newDir/newTest.png')) // Reference an image in the rawfile directory. 278``` 279 280### System Resources 281 282Apart from custom resources, you can use system resources to develop different applications with the same visual style. For details about the system resource IDs and their values in different configurations, see [Resources](../../design/ux-design/design-resources.md). 283 284During development, the usage of layered parameters is basically the same as that of qualifiers. To reference a system resource, use the "$r('sys.type.resource_id')" format. Wherein: **sys** indicates a system resource; **type** indicates the resource type, which can be **color**, **float**, **string**, or **media**; **resource_id** indicates the resource ID. 285 286> **NOTE** 287> 288> - The use of system resources is only supported in the declarative development paradigm. 289> 290> - For preset applications, you are advised to use system resources. For third-party applications, you can choose to use system resources or custom application resources as required. 291 292```ts 293Text('Hello') 294 .fontColor($r('sys.color.ohos_id_color_emphasize')) 295 .fontSize($r('sys.float.ohos_id_text_size_headline1')) 296 .fontFamily($r('sys.string.ohos_id_text_font_family_medium')) 297 .backgroundColor($r('sys.color.ohos_id_color_palette_aux1')) 298 299Image($r('sys.media.ohos_app_icon')) 300 .border({ 301 color: $r('sys.color.ohos_id_color_palette_aux1'), 302 radius: $r('sys.float.ohos_id_corner_radius_button'), width: 2 303 }) 304 .margin({ 305 top: $r('sys.float.ohos_id_elements_margin_horizontal_m'), 306 bottom: $r('sys.float.ohos_id_elements_margin_horizontal_l') 307 }) 308 .height(200) 309 .width(300) 310``` 311 312## Resource Matching 313 314When your application needs to use a resource, the system preferentially searches the qualifiers subdirectories that match the current device state. The system searches the **base** subdirectory for the target resource only when the **resources** directory does not contain any qualifiers subdirectories that match the current device state or the target resource is not found in the qualifiers subdirectories. The **rawfile** directory is not searched for resources. 315 316**Rules for Matching Qualifiers Subdirectories and Device Resources** 317 318- Qualifiers are matched with the device resources in the following priorities: MCC&MNC > locale (options: language, language_script, language_country/region, and language_script_country/region) > screen orientation > device type > color mode > screen density 319 320- If the qualifiers subdirectories contain the MCC, MNC, language, script, screen orientation, device type, and color mode qualifiers, their values must be consistent with the current device status so that the subdirectories can be used for matching the device resources. For example, the qualifiers subdirectory **zh_CN-car-ldpi** cannot be used for matching the resource files labeled **en_US**. 321 322For more information about how resources are loaded in applications, see the internationalization and localization documents. 323 324**Overlay Mechanism** 325 326Overylay is a resource replacement mechanism. With overlay resource packages, you enable your application GUI to adapt to different styles of various brands and products, without having to repack your application HAPs. The overylay mechanism works in dynamic and static modes. 327 328- Using overlay in dynamic mode 329 3301. Place the overlay resource package in the target application installation path. For example, for the com.example.overlay application, place the overlay resource package in **data/app/el1/bundle/public/com.example.overlay/**. 331 3322. The application uses [addResource(path)](../reference/apis-localization-kit/js-apis-resource-manager.md#addresource10) to load overlay resources and uses [removeResource(path)](../reference/apis-localization-kit/js-apis-resource-manager.md#removeresource10) to remove overlay resources. The path to an overlay resource consists of the application's sandbox root directory (obtained through **getContext().BundleCodeDir**) and the HSP name of the resource. For example, **let path = getContext().bundleCodeDir + "HSP name"**, such as **/data/storage/el1/bundle/enter-release-signed.hsp**. 333 334- Using overlay in static mode 335 336If the **module.json5** file of a module contains the **targetModuleName** and **targetPriority** fields during project creation on DevEco Studio, the module is identified as a module with the overlay feature in the installation phase. Modules with the overlay feature generally provide an overlay resource file for other modules on the device, so that the module specified by **targetModuleName** can display different colors, labels, themes, and the like by using the overlay resource file in a running phase. 337 338The overlay feature is enabled by default. For details about how to enable and disable this feature, see [@ohos.bundle.overlay (overlay)](../reference/apis-ability-kit/js-apis-overlay.md). 339 340