• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# 资源限定与访问<a name="ZH-CN_TOPIC_0000001164130752"></a>
2
3>![](../public_sys-resources/icon-note.gif) **说明:**
4>-   屏幕密度从API Version 4 开始支持。
5
6## 资源限定词<a name="zh-cn_topic_0000001173164731_section197802036142011"></a>
7
8资源限定词可以由一个或多个表征应用场景或设备特征的限定词组合而成,包括屏幕密度等维度,限定词之间通过中划线(-)连接。开发者在**resources**目录下创建限定词文件时,需要掌握限定词文件的命名要求以及与限定词文件与设备状态的匹配规则。
9
10## 资源限定词的命名要求<a name="zh-cn_topic_0000001173164731_section550585117202"></a>
11
12-   限定词的组合顺序:屏幕密度。开发者可以根据应用的使用场景和设备特征,选择其中的一类或几类限定词组成目录名称,顺序不可颠倒。
13-   限定词的连接方式:限定词之间均采用中划线(-)连接。例如:res-dark-ldpi.json14-   限定词的取值范围:每类限定词的取值必须符合下表的条件,否则,将无法匹配目录中的资源文件,限定词大小写敏感。
15-   限定词前缀:**resources**资源文件的资源限定词有前缀res,例如res-ldpi.json16-   默认资源限定文件:**resources**资源文件的默认资源限定文件为res-defaults.json17-   资源限定文件中不支持使用枚举格式的颜色来设置资源。
18
19**表 1**  资源限定词
20
21<a name="zh-cn_topic_0000001173164731_table153971631192110"></a>
22<table><thead align="left"><tr id="zh-cn_topic_0000001173164731_row1397133152114"><th class="cellrowborder" valign="top" width="23.400000000000002%" id="mcps1.2.3.1.1"><p id="zh-cn_topic_0000001173164731_p7397331152114"><a name="zh-cn_topic_0000001173164731_p7397331152114"></a><a name="zh-cn_topic_0000001173164731_p7397331152114"></a>类型</p>
23</th>
24<th class="cellrowborder" valign="top" width="76.6%" id="mcps1.2.3.1.2"><p id="zh-cn_topic_0000001173164731_p1139719318218"><a name="zh-cn_topic_0000001173164731_p1139719318218"></a><a name="zh-cn_topic_0000001173164731_p1139719318218"></a>含义与取值说明</p>
25</th>
26</tr>
27</thead>
28<tbody><tr id="zh-cn_topic_0000001173164731_row1890413245238"><td class="cellrowborder" valign="top" width="23.400000000000002%" headers="mcps1.2.3.1.1 "><p id="zh-cn_topic_0000001173164731_p2090532442315"><a name="zh-cn_topic_0000001173164731_p2090532442315"></a><a name="zh-cn_topic_0000001173164731_p2090532442315"></a>屏幕密度</p>
29</td>
30<td class="cellrowborder" valign="top" width="76.6%" headers="mcps1.2.3.1.2 "><p id="zh-cn_topic_0000001173164731_p2389144311231"><a name="zh-cn_topic_0000001173164731_p2389144311231"></a><a name="zh-cn_topic_0000001173164731_p2389144311231"></a>表示设备的屏幕密度(单位为dpi),取值如下:</p>
31<a name="zh-cn_topic_0000001173164731_ul1311184562317"></a><a name="zh-cn_topic_0000001173164731_ul1311184562317"></a><ul id="zh-cn_topic_0000001173164731_ul1311184562317"><li>ldpi:表示低密度屏幕(~120dpi)(0.75基准密度)</li><li>mdpi:表示中密度屏幕(~160dpi)(基准密度)</li><li>hdpi:表示高密度屏幕(~240dpi)(1.5基准密度)</li><li>xhdpi:表示加高密度屏幕(~320dpi)(2.0基准密度)</li><li>xxhdpi:表示超超高密度屏幕(~480dpi)(3.0基准密度)</li><li>xxxhdpi:表示超超超高密度屏幕(~640dpi)(4.0基准密度)</li></ul>
32</td>
33</tr>
34</tbody>
35</table>
36
37## 限定词与设备状态的匹配规则<a name="zh-cn_topic_0000001173164731_section59927387241"></a>
38
39-   在为设备匹配对应的资源文件时,限定词目录匹配屏幕密度。在资源限定词目录均未匹配的情况下,则匹配默认资源限定文件。
40-   如果限定词目录中包含资源限定词,则对应限定词的取值必须与当前的设备状态完全一致,该目录才能够参与设备的资源匹配。例如:资源限定文件res-hdpi.json与当前设备密度xhdpi无法匹配。
41
42## 引用JS模块内resources资源<a name="zh-cn_topic_0000001173164731_section7516798256"></a>
43
44在应用开发的hml和js文件中使用$r的语法,可以对JS模块内的resources目录下的json资源进行格式化,获取相应的资源内容。
45
46<a name="zh-cn_topic_0000001173164731_table1595144416585"></a>
47<table><thead align="left"><tr id="zh-cn_topic_0000001173164731_row1295119445588"><th class="cellrowborder" valign="top" width="11.41114111411141%" id="mcps1.1.4.1.1"><p id="zh-cn_topic_0000001173164731_p18951844195815"><a name="zh-cn_topic_0000001173164731_p18951844195815"></a><a name="zh-cn_topic_0000001173164731_p18951844195815"></a>属性</p>
48</th>
49<th class="cellrowborder" valign="top" width="23.17231723172317%" id="mcps1.1.4.1.2"><p id="zh-cn_topic_0000001173164731_p16951044135813"><a name="zh-cn_topic_0000001173164731_p16951044135813"></a><a name="zh-cn_topic_0000001173164731_p16951044135813"></a>类型</p>
50</th>
51<th class="cellrowborder" valign="top" width="65.41654165416541%" id="mcps1.1.4.1.3"><p id="zh-cn_topic_0000001173164731_p1795154417586"><a name="zh-cn_topic_0000001173164731_p1795154417586"></a><a name="zh-cn_topic_0000001173164731_p1795154417586"></a>描述</p>
52</th>
53</tr>
54</thead>
55<tbody><tr id="zh-cn_topic_0000001173164731_row8951164435812"><td class="cellrowborder" valign="top" width="11.41114111411141%" headers="mcps1.1.4.1.1 "><p id="zh-cn_topic_0000001173164731_p195124495814"><a name="zh-cn_topic_0000001173164731_p195124495814"></a><a name="zh-cn_topic_0000001173164731_p195124495814"></a>$r</p>
56</td>
57<td class="cellrowborder" valign="top" width="23.17231723172317%" headers="mcps1.1.4.1.2 "><p id="zh-cn_topic_0000001173164731_p3952164418589"><a name="zh-cn_topic_0000001173164731_p3952164418589"></a><a name="zh-cn_topic_0000001173164731_p3952164418589"></a>(key: string) =&gt; string</p>
58</td>
59<td class="cellrowborder" valign="top" width="65.41654165416541%" headers="mcps1.1.4.1.3 "><p id="zh-cn_topic_0000001173164731_p204636815564"><a name="zh-cn_topic_0000001173164731_p204636815564"></a><a name="zh-cn_topic_0000001173164731_p204636815564"></a>获取资源限定下具体的资源内容。例如:this.$r('strings.hello')。</p>
60<p id="zh-cn_topic_0000001173164731_p1146311815615"><a name="zh-cn_topic_0000001173164731_p1146311815615"></a><a name="zh-cn_topic_0000001173164731_p1146311815615"></a>参数说明:</p>
61<a name="zh-cn_topic_0000001173164731_ul18463178185617"></a><a name="zh-cn_topic_0000001173164731_ul18463178185617"></a><ul id="zh-cn_topic_0000001173164731_ul18463178185617"><li>key:定义在资源限定文件中的键值,如strings.hello。</li></ul>
62<p id="zh-cn_topic_0000001173164731_p174645835614"><a name="zh-cn_topic_0000001173164731_p174645835614"></a><a name="zh-cn_topic_0000001173164731_p174645835614"></a><strong id="zh-cn_topic_0000001173164731_b74640855613"><a name="zh-cn_topic_0000001173164731_b74640855613"></a><a name="zh-cn_topic_0000001173164731_b74640855613"></a>res-defaults.json示例:</strong></p>
63<pre class="screen" id="zh-cn_topic_0000001173164731_screen8464178165613"><a name="zh-cn_topic_0000001173164731_screen8464178165613"></a><a name="zh-cn_topic_0000001173164731_screen8464178165613"></a>{
64    strings: {
65        hello: 'hello world'
66    }
67}</pre>
68</td>
69</tr>
70</tbody>
71</table>
72
73## 示例<a name="zh-cn_topic_0000001173164731_section9710101017318"></a>
74
75resources/res-dark.json:
76
77```
78{
79    "image": {
80        "clockFace": "common/dark_face.png"
81    },
82    "colors": {
83	"background": "#000000"
84    }
85}
86```
87
88resources/res-defaults.json:
89
90```
91{
92    "image": {
93        "clockFace": "common/face.png"
94    },
95    "colors": {
96	"background": "#ffffff"
97    }
98}
99```
100
101```
102<!-- xxx.hml -->
103<div style="background-color: {{ $r('colors.background') }}">
104    <image src="{{ $r('image.clockFace') }}"></image>
105</div>
106```
107
108>![](../public_sys-resources/icon-note.gif) **说明:**
109>资源限定文件中不支持颜色枚举格式。
110
111