目录

Resource Limitations and Access

Resource Qualifiers

The name of a resource qualifier consists of one or more qualifiers that represent the application scenarios or device characteristics, covering the screen density and more. The qualifiers are separated using hyphens (-). When creating a qualifiers file under resources, you need to understand the file naming conventions and the rules for matching qualifiers files and the device status.

Naming Conventions for Resource Qualifiers

Table 1 Resource qualifiers

Type Description and Value Range
Screen density

Indicates the screen density of the device, in DPI. The value can be:

- ldpi: low-density screen (~120 dpi) (0.75 x Reference density)

- mdpi: medium-density screen (~160 dpi) (reference density)

- hdpi: high-density screen (~240 dpi) (1.5 x Reference density)

- xhdpi: extra high-density screen (~320 dpi) (2.0 x Reference density)

- xxhdpi: extra extra high-density screen (~480 dpi) (3.0 x Reference density)

- xxxhdpi: extra extra extra high-density screen (~640 dpi) (4.0 x Reference density)

Rules for Matching Qualifiers Files and Device Resources

Referencing Resources in the JS Module

You can use the $r syntax in the application development files .hml and .js to format the JSON resources in the resources directory of the JS module and obtain the corresponding resources. This directory is at the same level as the pages directory. For details about the directory structure, see File Organization.

Attribute Type Description
$r (key: string) => string

Obtains the resource content that matches the specific qualifiers, Example: $r('strings.hello')

Parameter description:

- key: key value defined in the resource qualifier file, for example, strings.hello.

Example of res-defaults.json:

{
    "strings": {
        "hello": "hello world"
    }
}

Example

resources/res-dark.json:

{
    "image": {
        "clockFace": "common/dark_face.png"
    },
    "colors": {
    "background": "#000000"
    }
}

resources/res-defaults.json:

{
    "image": {
        "clockFace": "common/face.png"
    },
    "colors": {
    "background": "#ffffff"
    }
}
<!-- xxx.hml -->
<div style="background-color: {{ $r('colors.background') }}">
    <image src="{{ $r('image.clockFace') }}"></image>
</div>

The resource qualifier file does not support color enumeration.