We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
Best PracticesOne-Time Development for Multi-Device DeploymentCross-Device UI DevelopmentCross-Device Resource Files

Cross-Device Resource Files

During page development, you may need to use different resources, such as colors, fonts, spacing, and images, based on target devices or configurations. To use different resources, you can follow the methods below.

  • Application resources: Configure device- or configuration-specific resources in the resource files.

  • System resources: Directly use the preset resource definitions (that is, layered parameters) in the system.

Application Resources

Resource Files

All custom resource files used during app development are stored in the resources directory of different modules for easy use and maintenance. The resources directory consists of two types of directories: base and qualifier directories, and rawfile directories. The following uses the entry module as an example to describe the base directory structure:

Collapse
Word wrap
Dark theme
Copy code
  1. └──entry/src/main/resources // Resources
  2. ├──base // Default directory
  3. │ ├──element
  4. │ │ ├──color.json // Color resources
  5. │ │ ├──float.json // Numeric resources
  6. │ │ └──string.json // String resources
  7. │ ├──media // Media resources
  8. │ └──profile // Routing resources
  9. ├──en_US // Example of the qualifiers directory, created by developers
  10. ├──zh_CN // Example of the qualifiers directory, created by developers
  11. └──rawfile // Raw file directory

The base directory exists by default, and the qualifiers directory needs to be created on your own. The name of a qualifiers directory may be a combination of one or more qualifiers that represent an application scenario or a device feature. When your app needs to use a resource, the system preferentially searches the qualifiers directories that match the current device state. The system searches the base directories for the target resource only when the resources directory does not contain any qualifiers directories that match the current device state or the target resource is not found in the qualifiers directories. The rawfile directory does not require device-state-based resource matching, so it is not covered in this section.

NOTE
  • For more information about the qualifiers directories, such as naming rules, creation process, and matching rules, go to Qualifiers Directory.

  • The system searches the base directories for the target resource only when no qualifiers directories that match the current device state are found or the target resource is absent from the qualifier directories. It is strongly recommended that default values be defined for all app-defined resources in the base directory to prevent exceptions where resources cannot be found.

  • The resource file path and resource qualifier usage for the web-like development paradigm are different from those of the declarative development paradigm. For details, see Resource Limitations and Access and File Organization.

You can create resource group directories (including element and media) under the base and qualifiers directories to store resource files of specific types.

Expand

Resource Group Directory

Description

Resource File

element

Indicates element resources. Each type of data is represented by a JSON file. The options are as follows:

- boolean: boolean data

- color: color data

- float: floating-point data

- intarray: array of integers

- integer: integer data

- pattern: pattern data

- plural: plural form data

- strarray: array of strings

- string: string

It is recommended that files in the element sub-directory be named the same as the following files, each of which can contain only data of the same type:

- boolean.json

- color.json

- float.json

- intarray.json

- integer.json

- pattern.json

- plural.json

- strarray.json

- string.json

media

Indicates media resources, including non-text files such as images, audios, and videos.

The file name can be customized, for example, icon.png.

In each resource file in the element directory, resources are defined in the name-value format, as shown below. In the media directory, the file name is used as the directory name. Therefore, you only need to place the file in the media directory without naming it.

Collapse
Word wrap
Dark theme
Copy code
  1. // color.json
  2. {
  3. "color": [
  4. {
  5. "name": "color_red",
  6. "value": "#FFFF0000"
  7. },
  8. {
  9. "name": "color_blue",
  10. "value": "#FF0000FF"
  11. }
  12. ]
  13. }

Accessing Application Resources

To reference an app resource in a project, use the "$r('app.type.name')" format. app indicates the resource defined in the resources directory of the app module. type indicates the resource type (or the location where the resource is stored), which can be color, float, string, plural, or media. name indicates the resource name, which is defined when you add the resource.

NOTE

For more details about resource access, see Resource Categories and Access.

Example

In the resources directory of the app module, create a qualifiers subdirectory named tablet, and add corresponding resources to the base and tablet subdirectories. Resources are listed in the below table.

Expand

Resource Name

Resource Type

Value in the base Directory

Value in the tablet Directory

my_string

string

default

tablet

my_color

color

#ff0000

#0000ff

my_float

float

60vp

80vp

my_image

media

my_image.png (Sun icon)

my_image.png (Moon icon)

Use app resources in the form of "$r('app.type.name')" in the code, and check the running effect on the default device and tablet. You can find that the values of the same resource on different devices are different.

Collapse
Word wrap
Dark theme
Copy code
  1. @Entry
  2. @Component
  3. struct ResourceQualifier {
  4. build() {
  5. Column() {
  6. Text($r('app.string.my_string'))
  7. .fontSize(60)
  8. .fontColor($r('app.color.my_color'))
  9. Image($r('app.media.my_image'))
  10. .width(100)
  11. .height(100)
  12. }
  13. .height('100%')
  14. .width('100%')
  15. .alignItems(HorizontalAlign.Center)
  16. .justifyContent(FlexAlign.Center)
  17. }
  18. }

System Resources

In addition to custom resources, preset system resources are also known as layered parameters, where each resource ID has different values under different configurations such as the device type and dark and light modes.

During development, the usage of layered parameters is basically the same as that of qualifiers. Reference a system resource using the "$r('sys.type.resource_id')" format. sys indicates a system resource. type indicates the resource type, which can be color, float, string, or media. resource_id indicates the resource ID.

NOTE
  • Only the declarative development paradigm supports layered parameters.

  • System resources ensure apps developed by different teams maintain a consistent visual style. For preinstalled system apps, you are advised to use system resources. For third-party apps, you can use system resources or custom app resources as required.