1 page.title=Cung cp Ti nguyn 2 parent.title=Ti nguyn ng dng 3 parent.link=index.html 4 @jd:body 5 6 <div id="qv-wrapper"> 7 <div id="qv"> 8 <h2>Xem nhanh</h2> 9 <ul> 10 <li>Cc loi ti nguyn khc nhau thuc v cc th mc con khc nhau ca {@code res/}</li> 11 <li>Ti nguyn thay th cung cp cc tp ti nguyn theo cu hnh c th</li> 12 <li>Lun bao gm ti nguyn mc nh ng dng ca bn khng ph thuc vo cc 13 cu hnh thit b c th</li> 14 </ul> 15 <h2>Trong ti liu ny</h2> 16 <ol> 17 <li><a href="#ResourceTypes">Nhm cc Loi Ti nguyn li</a></li> 18 <li><a href="#AlternativeResources">Cung cp Ti nguyn Thay th</a> 19 <ol> 20 <li><a href="#QualifierRules">Quy tc v tn hn nh</a></li> 21 <li><a href="#AliasResources">To ti nguyn b danh</a></li> 22 </ol> 23 </li> 24 <li><a href="#Compatibility">Cung cp Tnh tng thch gia Thit b vi Ti nguyn Tt nht</a></li> 25 <li><a href="#BestMatch">Cch Android tm Ti nguyn Khp Tt nht</a></li> 26 </ol> 27 28 <h2>Xem thm</h2> 29 <ol> 30 <li><a href="accessing-resources.html">Truy cp Ti nguyn</a></li> 31 <li><a href="available-resources.html">Loi Ti nguyn</a></li> 32 <li><a href="{@docRoot}guide/practices/screens_support.html">H tr Nhiu 33 Mn hnh</a></li> 34 </ol> 35 </div> 36 </div> 37 38 <p>Bn nn lun ngoi hin ha cc ti nguyn ng dng chng hn nh hnh nh v xu t m 39 ca mnh, sao cho bn c th duy tr chng mt cch c lp. Bn cng nn cung cp ti nguyn thay th cho 40 cu hnh thit b c th bng cch nhm chng li trong nhng th mc ti nguyn ch danh. Trong 41 thi gian chy, Android s s dng ti nguyn ph hp da trn cu hnh hin ti. V 42 d, bn c th mun cung cp mt b tr UI khc ph thuc vo kch c mn hnh hoc cc xu 43 khc nhau ph thuc vo thit t ngn ng.</p> 44 45 <p>Sau khi ngoi hin ha cc ti nguyn ng dng ca mnh, bn c th truy cp chng 46 bng cch s dng cc ID ti nguyn c khi to trong lp {@code R} ca d n ca bn. Cch s dng 47 ti nguyn trong ng dng ca bn c trnh by trong phn <a href="accessing-resources.html">Truy cp 48 Ti nguyn</a>. Ti liu ny trnh by vi bn cch nhm cc ti nguyn li trong d n Android ca bn v 49 cung cp ti nguyn thay th cho nhng cu hnh thit b c th.</p> 50 51 52 <h2 id="ResourceTypes">Nhm cc Loi Ti nguyn li</h2> 53 54 <p>Bn nn t tng loi ti nguyn vo mt th mc con c th trong th mc 55 {@code res/} d n ca mnh. V d, sau y l phn cp tp ca mt d n n gin:</p> 56 57 <pre class="classic no-pretty-print"> 58 MyProject/ 59 src/ <span style="color:black"> 60 MyActivity.java </span> 61 res/ 62 drawable/ <span style="color:black"> 63 graphic.png </span> 64 layout/ <span style="color:black"> 65 main.xml 66 info.xml</span> 67 mipmap/ <span style="color:black"> 68 icon.png </span> 69 values/ <span style="color:black"> 70 strings.xml </span> 71 </pre> 72 73 <p>Nh bn c th thy trong v d ny, th mc {@code res/} cha tt c ti nguyn (trong 74 cc th mc con): mt ti nguyn hnh nh, hai ti nguyn b tr, cc th mc{@code mipmap/} cho biu tng ca trnh khi chy 75 , v mt tp ti nguyn xu. Tn th mc 76 ti nguyn c vai tr quan trng v c m t trong bng 1.</p> 77 78 <p class="note"><strong>Lu :</strong> bit thm thng tin v cch s dng th mc mipmap, hy xem phn 79 <a href="{@docRoot}tools/projects/index.html#mipmap">Tng quan v Qun l D n</a>.</p> 80 81 <p class="table-caption" id="table1"><strong>Bng 1.</strong> Cc th mc ti nguyn 82 c h tr bn trong th mc {@code res/} ca d n.</p> 83 84 <table> 85 <tr> 86 <th scope="col">Th mc</th> 87 <th scope="col">Loi Ti nguyn</th> 88 </tr> 89 90 <tr> 91 <td><code>animator/</code></td> 92 <td>Tp XML nh ngha <a href="{@docRoot}guide/topics/graphics/prop-animation.html">cc hot hnh 93 tnh cht</a>.</td> 94 </tr> 95 96 <tr> 97 <td><code>anim/</code></td> 98 <td>Tp XML nh ngha <a href="{@docRoot}guide/topics/graphics/view-animation.html#tween-animation">cc hot hnh 99 tween</a>. (Cc hot hnh tnh cht cng c th c lu trong th mc ny, nhng 100 th mc {@code animator/} c u tin cho hot hnh tnh cht phn bit gia hai 101 loi ny.)</td> 102 </tr> 103 104 <tr> 105 <td><code>color/</code></td> 106 <td>Tp XML nh ngha mt danh sch trng thi cc mu. Xem phn <a href="color-list-resource.html">Ti nguyn 107 Danh sch Trng thi Mu</a></td> 108 </tr> 109 110 <tr> 111 <td><code>drawable/</code></td> 112 113 <td><p>Tp bitmap ({@code .png}, {@code .9.png}, {@code .jpg}, {@code .gif}) hoc tp XML 114 c bin dch thnh cc loi ti nguyn con v c sau:</p> 115 <ul> 116 <li>Tp bitmap</li> 117 <li>Nine-Patche (tp bitmap c th thay i kch c)</li> 118 <li>Danh sch trng thi</li> 119 <li>Hnh</li> 120 <li>Ni dung v c hot hnh</li> 121 <li>Ni dung v c khc</li> 122 </ul> 123 <p>Xem phn <a href="drawable-resource.html">Ti nguyn V c</a>.</p> 124 </td> 125 </tr> 126 127 <tr> 128 <td><code>mipmap/</code></td> 129 <td>Tp v c cho cc mt biu tng trnh khi chy khc nhau. bit thm thng tin v vic qun l 130 cc biu tng trnh khi chy bng th mc {@code mipmap/}, xem phn 131 <a href="{@docRoot}tools/project/index.html#mipmap">Tng quan v Qun l D n</a>.</td> 132 </tr> 133 134 <tr> 135 <td><code>layout/</code></td> 136 <td>Tp XML nh ngha mt b tr giao din ngi dng. 137 Xem phn <a href="layout-resource.html">Ti nguyn B tr</a>.</td> 138 </tr> 139 140 <tr> 141 <td><code>menu/</code></td> 142 <td>Tp XML nh ngha cc menu ng dng, chng hn nh Menu Ty chn, Menu Ng cnh, hoc Menu 143 Con. Xem phn <a href="menu-resource.html">Ti nguyn Menu</a>.</td> 144 </tr> 145 146 <tr> 147 <td><code>raw/</code></td> 148 <td><p>Tp ty lu trong dng th ca chng. m nhng ti nguyn c mt 149 {@link java.io.InputStream} th ny, hy gi {@link android.content.res.Resources#openRawResource(int) 150 Resources.openRawResource()} bng ID ti nguyn, chnh l {@code R.raw.<em>filename</em>}.</p> 151 <p>Tuy nhin, nu cn truy cp tn tp gc v phn cp tp, bn c th xem xt 152 lu mt s ti nguyn trong th mc {@code 153 assets/} (thay v {@code res/raw/}). Cc tp trong {@code assets/} khng c cp 154 ID ti nguyn, v th bn ch c th c chng bng cch s dng {@link android.content.res.AssetManager}.</p></td> 155 </tr> 156 157 <tr> 158 <td><code>values/</code></td> 159 <td><p>Tp XML cha cc gi tr n gin, chng hn nh xu, s nguyn, v mu sc.</p> 160 <p>Trong , tp ti nguyn XML trong cc th mc con {@code res/} khc nh ngha mt ti nguyn n l 161 da trn tn tp XML, tp trong th mc {@code values/} s m t nhiu ngun. 162 i vi tp trong th mc ny, mi phn t con ca phn t {@code <resources>} li nh ngha mt ti nguyn 163 duy nht. V d, phn t {@code <string>} to ti nguyn 164 {@code R.string} v phn t {@code <color>} to ti nguyn {@code R.color} 165 .</p> 166 <p>V mi ti nguyn c nh ngha bng phn t XML ca chnh n, bn c th t tn tp 167 theo cch mnh mun v t cc loi ti nguyn khc nhau vo mt tp. Tuy nhin, gii thch r, bn c th 168 mun t cc loi ti nguyn duy nht vo nhng tp khc nhau. V d, sau y l mt s quy c 169 tn tp cho cc ti nguyn m bn c th to trong th mc ny:</p> 170 <ul> 171 <li>arrays.xml cho mng ti nguyn (<a href="more-resources.html#TypedArray">mng c phn loi</a>).</li> 172 <li>colors.xml cho <a href="more-resources.html#Color">gi tr mu</a></li> 173 <li>dimens.xml cho <a href="more-resources.html#Dimension">gi tr kch thc</a>.</li> 174 <li>strings.xml cho <a href="string-resource.html">gi tr 175 xu</a>.</li> 176 <li>styles.xml cho <a href="style-resource.html">kiu</a>.</li> 177 </ul> 178 <p>Xem cc phn <a href="string-resource.html">Ti nguyn Xu</a>, 179 <a href="style-resource.html">Ti nguyn Kiu</a>, v 180 <a href="more-resources.html">cc Loi Ti nguyn khc</a>.</p> 181 </td> 182 </tr> 183 184 <tr> 185 <td><code>xml/</code></td> 186 <td>Tp XML ty m c th c c vo thi gian chy bng cch gi {@link 187 android.content.res.Resources#getXml(int) Resources.getXML()}. Cc tp cu hnh XML khc nhau 188 phi c lu y, chng hn nh mt <a href="{@docRoot}guide/topics/search/searchable-config.html">cu hnh c th tm kim</a>. 189 <!-- or preferences configuration. --></td> 190 </tr> 191 </table> 192 193 <p class="caution"><strong>Ch :</strong> Khng c lu tp ti nguyn trc tip vo trong th mc 194 {@code res/}—n s gy ra li vi trnh bin dch.</p> 195 196 <p> bit thm thng tin v cc loi ti nguyn, hy xem ti liu <a href="available-resources.html">Cc Loi Ti nguyn</a>.</p> 197 198 <p>Ti nguyn m bn lu trong th mc con c nh ngha trong bng 1 l nhng ti nguyn "mc nh" 199 ca bn. C th, nhng ti nguyn ny nh ngha thit k v ni dung mc nh cho ng dng ca bn. 200 Tuy nhin, cc loi thit b da trn nn tng Android khc nhau c th gi cc loi ti nguyn khc nhau. 201 V d, nu mt thit b c mt mn hnh ln hn bnh thng, khi bn nn cung cp 202 cc ti nguyn b tr khc nhau tn dng din tch mn hnh tng thm. Hoc, nu mt thit b c 203 thit t ngn ng khc, khi bn nn cung cp cc ti nguyn xu khc bin dch 204 vn bn trong giao din ngi dng ca mnh. cung cp nhng ti nguyn khc nhau ny cho cc cu hnh 205 thit b khc nhau, bn cn cung cp ti nguyn thay th bn cnh nhng ti nguyn 206 mc nh ca mnh.</p> 207 208 209 <h2 id="AlternativeResources">Cung cp Ti nguyn Thay th</h2> 210 211 212 <div class="figure" style="width:429px"> 213 <img src="{@docRoot}images/resources/resource_devices_diagram2.png" height="167" alt="" /> 214 <p class="img-caption"> 215 <strong>Hnh 1.</strong> Hai thit b khc nhau, mi thit b s dng cc ti nguyn b tr khc nhau.</p> 216 </div> 217 218 <p>Hu nh mi ng dng u nn cung cp cc ti nguyn thay th h tr nhng cu hnh 219 thit b c th. V d, bn nn bao gm cc ti nguyn v c thay th cho cc mt 220 mn hnh khc nhau v ti nguyn xu thay th cho cc ngn ng khc nhau. Vo thi gian chy, Android 221 s pht hin cu hnh thit b hin ti v ti cc ti nguyn 222 tng ng cho ng dng ca bn.</p> 223 224 <p> quy nh cc phng n thay th theo cu hnh c th cho mt tp hp ti nguyn:</p> 225 <ol> 226 <li>To mt th mc mi trong {@code res/} c tn theo dng {@code 227 <em><resources_name></em>-<em><config_qualifier></em>}. 228 <ul> 229 <li><em>{@code <resources_name>}</em> l tn th mc ca cc ti nguyn mc nh tng ng 230 (c nh ngha trong bng 1).</li> 231 <li><em>{@code <qualifier>}</em> l tn quy nh mt cu hnh ring 232 m nhng ti nguyn ny s c s dng cho n (c nh ngha trong bng 2).</li> 233 </ul> 234 <p>Bn c th ni nhiu hn mt <em>{@code <qualifier>}</em>. Tch ring tng ci 235 bng mt nt gch.</p> 236 <p class="caution"><strong>Ch :</strong> Khi ni nhiu hn nh, bn phi 237 t chng theo cng th t lit k trong bng 2. Nu cc hn nh c xp th t 238 sai, ti nguyn s b b qua.</p> 239 </li> 240 <li>Lu cc ti nguyn thay th tng ng vo th mc mi ny. Tp ti nguyn phi c 241 t tn ng nh cc tp ti nguyn mc nh.</li> 242 </ol> 243 244 <p>V d, sau y l mt s ti nguyn mc nh v thay th:</p> 245 246 <pre class="classic no-pretty-print"> 247 res/ 248 drawable/ <span style="color:black"> 249 icon.png 250 background.png </span> 251 drawable-hdpi/ <span style="color:black"> 252 icon.png 253 background.png </span> 254 </pre> 255 256 <p>Hn nh {@code hdpi} cho bit rng cc ti nguyn trong th mc p dng cho nhng thit b c 257 mn hnh mt cao. Hnh nh trong tng th mc v c ny c nh c cho mt mt 258 mn hnh c th, nhng tn tp th 259 ging ht. Bng cch ny, ID ti nguyn m bn s dng tham chiu {@code icon.png} hoc hnh nh {@code 260 background.png} lun nh nhau, nhng Android s chn 261 phin bn ca tng ti nguyn cho khp tt nht vi thit b hin ti, bng cch so snh thng tin cu hnh thit b 262 vi cc hn nh v tn th mc ti nguyn.</p> 263 264 <p>Android h tr mt vi hn nh cu hnh v bn c th 265 thm nhiu hn nh vo mt tn th mc, bng cch tch ring tng hn nh bng mt nt gch. Bng 2 266 lit k cc hn nh cu hnh hp l, theo th t u tin—nu bn s dng nhiu 267 hn nh cho mt th mc ti nguyn, bn phi thm chng vo tn th mc theo th t c lit k trong 268 bng.</p> 269 270 271 <p class="table-caption" id="table2"><strong>Bng 2.</strong> Tn ca hn nh 272 cu hnh.</p> 273 <table> 274 <tr> 275 <th>Cu hnh</th> 276 <th>Gi tr Hn nh</th> 277 <th>M t</th> 278 </tr> 279 <tr id="MccQualifier"> 280 <td>MCC v MNC</td> 281 <td>V d:<br/> 282 <code>mcc310</code><br/> 283 <code><nobr>mcc310-mnc004</nobr></code><br/> 284 <code>mcc208-mnc00</code><br/> 285 v.v. 286 </td> 287 <td> 288 <p>M quc gia di ng (MCC), ng sau c th l m mng di ng (MNC) 289 t th SIM trong thit b. V d, <code>mcc310</code> Hoa K i vi mi nh mng, 290 <code>mcc310-mnc004</code> Hoa K i vi Verizon, v <code>mcc208-mnc00</code> Php i vi 291 Orange.</p> 292 <p>Nu thit b s dng mt kt ni v tuyn (in thoi GSM), cc gi tr MCC v MNC s ly 293 t th SIM.</p> 294 <p>Bn cng c th s dng ch MCC (v d, a cc ti nguyn 295 php l theo quc gia c th vo ng dng ca bn). Nu bn cn quy nh ch da trn ngn ng, hy s dng hn nh 296 <em>ngn ng v khu vc</em> thay th (c trnh by phn tip theo). Nu bn quyt nh s dng hn nh MCC v 297 MNC, bn nn cn thn v kim tra xem n c hot ng nh k vng khng.</p> 298 <p>Ngoi ra, cng xem cc trng cu hnh {@link 299 android.content.res.Configuration#mcc}, v {@link 300 android.content.res.Configuration#mnc}, tng ng cho bit m quc gia di ng v 301 m mng di ng hin ti.</p> 302 </td> 303 </tr> 304 <tr id="LocaleQualifier"> 305 <td>Ngn ng v khu vc</td> 306 <td>V d:<br/> 307 <code>en</code><br/> 308 <code>fr</code><br/> 309 <code>en-rUS</code><br/> 310 <code>fr-rFR</code><br/> 311 <code>fr-rCA</code><br/> 312 v.v. 313 </td> 314 <td><p>Ngn ng c nh ngha bng mt m ngn ng <a href="http://www.loc.gov/standards/iso639-2/php/code_list.php">ISO 315 639-1</a> gm hai ch ci, c th theo sau l mt m khu vc 316 <a href="http://www.iso.org/iso/en/prods-services/iso3166ma/02iso-3166-code-lists/list-en1.html">ISO 317 3166-1-alpha-2</a> di hai ch ci (ng trc l "{@code r}" ch thng). 318 </p><p> 319 Cc m <em>khng</em> phn bit ch hoa/thng; tin t {@code r} c s dng 320 phn bit phn khu vc. 321 Bn khng th ch quy nh mt khu vc.</p> 322 <p>iu ny c th thay i trong sut vng i 323 ng dng ca bn nu ngi dng thay i ngn ng ca mnh trong ci t h thng. Xem phn <a href="runtime-changes.html">X l Thay i Thi gian chy</a> bit thng tin v 324 nh hng c th c ca thay i ny ti ng dng ca bn trong thi gian chy.</p> 325 <p>Xem phn <a href="localization.html">Bn a ha</a> bit hng dn y v vic bn a ha 326 ng dng ca bn cho cc ngn ng khc.</p> 327 <p>Xem thm trng cu hnh {@link android.content.res.Configuration#locale}, trong 328 cho bit a phng hin ti.</p> 329 </td> 330 </tr> 331 <tr id="LayoutDirectionQualifier"> 332 <td>Ch hng B tr</td> 333 <td><code>ldrtl</code><br/> 334 <code>ldltr</code><br/> 335 </td> 336 <td><p>Ch hng b tr ca ng dng ca bn. {@code ldrtl} c ngha l "ch-hng-b-tr-phi-qua-tri". 337 {@code ldltr} c ngha l "ch-hng-b-tr-tri-qua-phi" v l gi tr khng biu th mc nh. 338 </p> 339 <p>iu ny c th p dng cho bt k ti nguyn no, chng hn nh b tr, ni dung v c hoc gi tr. 340 </p> 341 <p>V d, nu bn mun cung cp mt b tr c th cho ngn ng -rp v mt 342 b tr chung no cho bt k ngn ng phi-qua-tri" no khc (nh ch Ba T hoc Do Thi), vy bn s phi: 343 </p> 344 <pre class="classic no-pretty-print"> 345 res/ 346 layout/ <span style="color:black"> 347 main.xml </span>(Default layout) 348 layout-ar/ <span style="color:black"> 349 main.xml </span>(Specific layout for Arabic) 350 layout-ldrtl/ <span style="color:black"> 351 main.xml </span>(Any "right-to-left" language, except 352 for Arabic, because the "ar" language qualifier 353 has a higher precedence.) 354 </pre> 355 <p class="note"><strong>Lu :</strong> kch hot cc tnh nng b tr phi-qua-tri 356 cho ng dng ca mnh, bn phi t <a href="{@docRoot}guide/topics/manifest/application-element.html#supportsrtl">{@code 357 supportsRtl}</a> thnh {@code "true"} v t <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#target">{@code targetSdkVersion}</a> thnh 17 tr ln.</p> 358 <p><em>c thm trong API mc 17.</em></p> 359 </td> 360 </tr> 361 <tr id="SmallestScreenWidthQualifier"> 362 <td>smallestWidth</td> 363 <td><code>sw<N>dp</code><br/><br/> 364 V d:<br/> 365 <code>sw320dp</code><br/> 366 <code>sw600dp</code><br/> 367 <code>sw720dp</code><br/> 368 v.v. 369 </td> 370 <td> 371 <p>Kch c c bn ca mt mn hnh, th hin bng kch thc ngn nht ca khu vc mn hnh 372 kh dng. C th, smallestWidth ca thit b bng khong ngn nht gia chiu cao v chiu rng 373 kh dng ca mn hnh (bn cng c th gi l "chiu rng nh nht c th" cho mn hnh). Bn c th 374 s dng hn nh ny m bo rng, khng ph thuc vo hng hin ti ca mn hnh, ng dng 375 ca bn c t nht {@code <N>} dp chiu rng kh dng cho UI ca mnh.</p> 376 <p>V d, nu b tr ca bn yu cu rng kch thc nh nht ca khu vc mn hnh ti thiu 377 phi lun bng 600 dp, vy bn c th s dng hn nh ny to cc ti nguyn b tr, {@code 378 res/layout-sw600dp/}. H thng s ch s dng nhng ti nguyn ny khi kch thc nh nht ca 379 mn hnh kh dng ti thiu bng 600dp, khng ph thuc vo cnh 600dp l chiu cao hay chiu rng 380 theo nhn thc ca ngi dng. SmallestWidth l c trng kch c mn hnh c nh ca thit b; <strong>smallestWidth ca 381 thit b khng thay i khi hng ca mn hnh thay i</strong>.</p> 382 <p>SmallestWidth ca mt thit b s xem xt c trang tr mn hnh v UI h thng. V 383 d, nu thit b c mt s phn t UI c nh trn mn hnh m chim mt khong trng dc 384 theo trc smallestWidth, h thng s khai bo smallestWidth nh hn kch c mn hnh 385 thc t, bi chng l nhng im nh mn hnh khng kh dng cho UI ca bn. V th, gi tr m bn s dng 386 nn l kch thc nh nht thc t <em>m b tr ca bn yu cu</em> (thng thng, gi tr ny bng 387 "chiu rng nh nht" m b tr ca bn h tr, khng ph thuc vo hng hin ti ca mn hnh).</p> 388 <p>Mt s gi tr m bn c th s dng y i vi cc kch c mn hnh ph bin:</p> 389 <ul> 390 <li>320, cho cc thit b c cu hnh mn hnh nh: 391 <ul> 392 <li>240x320 ldpi (thit b cm tay QVGA)</li> 393 <li>320x480 mdpi (thit b cm tay)</li> 394 <li>480x800 hdpi (thit b cm tay mt cao)</li> 395 </ul> 396 </li> 397 <li>480, i vi nhng mn hnh nh 480x800 mdpi (my tnh bng/thit b cm tay).</li> 398 <li>600, i vi nhng mn hnh nh 600x1024 mdpi (my tnh bng 7").</li> 399 <li>720, i vi nhng mn hnh nh 720x1280 mdpi (my tnh bng 10").</li> 400 </ul> 401 <p>Khi ng dng ca bn cung cp nhiu th mc ti nguyn vi nhng gi tr khc nhau cho 402 hn nh smallestWidth, h thng s s dng hn nh gn nht vi (khng vt qu) 403 smallestWidth ca thit b. </p> 404 <p><em>c thm trong API mc 13.</em></p> 405 <p>Xem thm thuc tnh <a href="{@docRoot}guide/topics/manifest/supports-screens-element.html#requiresSmallest">{@code 406 android:requiresSmallestWidthDp}</a>, trong khai bo smallestWidth ti thiu m ng dng ca bn 407 tng thch vi, v trng cu hnh {@link 408 android.content.res.Configuration#smallestScreenWidthDp}, trong lu tr gi tr 409 smallestWidth ca thit b.</p> 410 <p> bit thm thng tin v vic thit k cho cc mn hnh khc nhau v s dng hn nh 411 ny, hy xem hng dn dnh cho nh pht trin <a href="{@docRoot}guide/practices/screens_support.html">H tr 412 Nhiu Mn hnh</a>.</p> 413 </td> 414 </tr> 415 <tr id="ScreenWidthQualifier"> 416 <td>Chiu rng kh dng</td> 417 <td><code>w<N>dp</code><br/><br/> 418 V d:<br/> 419 <code>w720dp</code><br/> 420 <code>w1024dp</code><br/> 421 v.v. 422 </td> 423 <td> 424 <p>Quy nh mt chiu rng mn hnh kh dng ti thiu theo n v {@code dp} m ti , ti nguyn 425 nn c s dng—c nh ngha bi gi tr <code><N></code>. Gi tr 426 cu hnh ny s thay i khi hng 427 thay i gia kh ngang v dc khp vi chiu rng thc t hin ti.</p> 428 <p>Khi ng dng ca bn cung cp nhiu th mc ti nguyn vi nhng gi tr khc nhau 429 cho cu hnh ny, h thng s s dng gi tr gn nht vi (khng vt qu) 430 chiu rng hin ti ca mn hnh. Gi tr 431 y xt c trang tr trn mn hnh, v th nu thit b c mt s 432 phn t UI c nh cnh tri hoc phi ca mn hnh, n 433 s s dng mt gi tr cho chiu rng nh hn kch c mn hnh thc s, dng 434 cho nhng phn t UI ny v lm gim khong trng kh dng ca ng dng.</p> 435 <p><em>c thm trong API mc 13.</em></p> 436 <p>Xem thm trng cu hnh {@link android.content.res.Configuration#screenWidthDp} 437 m cha chiu rng mn hnh hin ti.</p> 438 <p> bit thm thng tin v vic thit k cho cc mn hnh khc nhau v s dng hn nh 439 ny, hy xem hng dn dnh cho nh pht trin <a href="{@docRoot}guide/practices/screens_support.html">H tr 440 Nhiu Mn hnh</a>.</p> 441 </td> 442 </tr> 443 <tr id="ScreenHeightQualifier"> 444 <td>Chiu cao kh dng</td> 445 <td><code>h<N>dp</code><br/><br/> 446 V d:<br/> 447 <code>h720dp</code><br/> 448 <code>h1024dp</code><br/> 449 v.v. 450 </td> 451 <td> 452 <p>Quy nh chiu cao mn hnh kh dng ti thiu theo n v "dp" m ti ti nguyn 453 nn c s dng—c nh ngha bi gi tr <code><N></code>. Gi tr 454 cu hnh ny s thay i khi hng 455 thay i gia kh ngang v dc khp vi chiu cao thc t hin ti.</p> 456 <p>Khi ng dng ca bn cung cp nhiu th mc ti nguyn vi nhng gi tr khc nhau 457 cho cu hnh ny, h thng s s dng gi tr gn nht vi (khng vt qu) 458 chiu cao hin ti ca mn hnh. Gi tr 459 y xt c trang tr trn mn hnh, v th nu thit b c mt s 460 phn t UI c nh trn cnh trn hoc di ca mn hnh, n s s dng 461 mt gi tr cho chiu cao nh hn kch c mn hnh thc s, dng 462 cho nhng phn t UI ny v lm gim khong trng kh dng ca ng dng. Trang tr 463 trn mn hnh m khng c nh (chng hn nh thanh trng thi ca in thoi m c th c 464 n khi ton mn hnh) <em>khng</em> c xt y, c 465 nhng trang tr trn ca s nh thanh tiu hay thanh hnh ng cng vy, v th ng dng phi c chun b 466 x l mt khong trng nh hn mc m chng quy nh. 467 <p><em>c thm trong API mc 13.</em></p> 468 <p>Xem thm trng cu hnh {@link android.content.res.Configuration#screenHeightDp} 469 m cha chiu rng mn hnh hin ti.</p> 470 <p> bit thm thng tin v vic thit k cho cc mn hnh khc nhau v s dng hn nh 471 ny, hy xem hng dn dnh cho nh pht trin <a href="{@docRoot}guide/practices/screens_support.html">H tr 472 Nhiu Mn hnh</a>.</p> 473 </td> 474 </tr> 475 <tr id="ScreenSizeQualifier"> 476 <td>Kch c mn hnh</td> 477 <td> 478 <code>small</code><br/> 479 <code>normal</code><br/> 480 <code>large</code><br/> 481 <code>xlarge</code> 482 </td> 483 <td> 484 <ul class="nolist"> 485 <li>{@code small}: Cc mn hnh c kch c tng t nh mn hnh 486 QVGA mt thp. Kch c b tr ti thiu i vi mt mn hnh nh 487 bng xp x 320x426 n v dp. Cc v d nh QVGA mt thp v VGA mt 488 cao.</li> 489 <li>{@code normal}: Cc mn hnh c kch c tng t nh mn hnh 490 HVGA mt trung bnh. Kch c b tr ti thiu 491 i vi mt mn hnh bnh thng bng xp x 320x470 n v dp. V d 492 v nhng mn hnh nh vy l WQVGA mt thp, HVGA mt trung bnh, WVGA 493 mt cao.</li> 494 <li>{@code large}: Cc mn hnh c kch c tng t nh mn hnh 495 VGA mt trung bnh. 496 Kch c b tr ti thiu i vi mt mn hnh ln bng xp x 480x640 n v dp. 497 V d nh cc mn hnh mt trung bnh VGA v WVGA.</li> 498 <li>{@code xlarge}: Cc mn hnh ln hn ng k so vi mn hnh 499 HVGA mt trung bnh truyn thng. Kch c b tr ti thiu i vi mt mn hnh siu ln 500 bng xp x 720x960 n v dp. Trong hu ht trng hp, nhng thit b c mn hnh 501 siu ln s qu ln mang trong ti v gn nh l 502 thit b kiu my tnh bng. <em>c thm trong API mc 9.</em></li> 503 </ul> 504 <p class="note"><strong>Lu :</strong> Vic s dng mt hn nh kch c khng hm rng cc 505 ti nguyn <em>ch</em> p dng cho mn hnh c kch c . Nu bn khng cung cp cho cc ti nguyn 506 thay th vi cc hn nh khp tt hn vi cu hnh thit b hin ti, h thng c th s dng 507 bt k ti nguyn no <a href="#BestMatch">ph hp nht</a>.</p> 508 <p class="caution"><strong>Ch :</strong> Nu tt c ti nguyn ca bn s dng mt hn nh kch c 509 <em>ln hn</em> mn hnh hin ti, h thng s <strong>khng</strong> s dng chng v 510 ng dng ca bn s b li vo thi gian chy (v d, nu tt c ti nguyn b tr c gn th hn nh {@code 511 xlarge} nhng thit b li c mn hnh kch c bnh thng).</p> 512 <p><em>c thm trong API mc 4.</em></p> 513 514 <p>Xem <a href="{@docRoot}guide/practices/screens_support.html">H tr Nhiu 515 Mn hnh</a> bit thm thng tin.</p> 516 <p>Xem thm trng cu hnh {@link android.content.res.Configuration#screenLayout}, 517 cho bit mn hnh l mn hnh nh, bnh thng, 518 hay ln.</p> 519 </td> 520 </tr> 521 <tr id="ScreenAspectQualifier"> 522 <td>T l mn hnh</td> 523 <td> 524 <code>long</code><br/> 525 <code>notlong</code> 526 </td> 527 <td> 528 <ul class="nolist"> 529 <li>{@code long}: Mn hnh di, chng hn nh WQVGA, WVGA, FWVGA</li> 530 <li>{@code notlong}: Mn hnh khng di, chng hn nh QVGA, HVGA v VGA</li> 531 </ul> 532 <p><em>c thm trong API mc 4.</em></p> 533 <p>Gi tr ny thun ty c da trn t l khung nh ca mn hnh (mn hnh "di" s rng hn). N 534 khng lin quan ti hng ca mn hnh.</p> 535 <p>Xem thm trng cu hnh {@link android.content.res.Configuration#screenLayout}, 536 cho bit mn hnh c di khng.</p> 537 </td> 538 </tr> 539 <tr id="OrientationQualifier"> 540 <td>Hng ca mn hnh</td> 541 <td> 542 <code>port</code><br/> 543 <code>land</code> <!-- <br/> 544 <code>square</code> --> 545 </td> 546 <td> 547 <ul class="nolist"> 548 <li>{@code port}: Thit b hng ng (thng ng)</li> 549 <li>{@code land}: Thit b kh ngang (nm ngang)</li> 550 <!-- Square mode is currently not used. --> 551 </ul> 552 <p>Gi tr ny c th thay i trong sut vng i ng dng ca bn nu ngi dng xoay 553 mn hnh. Xem phn <a href="runtime-changes.html">X l Thay i Thi gian chy</a> bit thng tin v 554 nh hng ca iu ny ti ng dng ca bn trong thi gian chy.</p> 555 <p>Xem thm trng cu hnh {@link android.content.res.Configuration#orientation}, trong 556 cho bit hng thit b hin ti.</p> 557 </td> 558 </tr> 559 <tr id="UiModeQualifier"> 560 <td>Ch UI</td> 561 <td> 562 <code>car</code><br/> 563 <code>desk</code><br/> 564 <code>television</code><br/> 565 <code>appliance</code> 566 <code>watch</code> 567 </td> 568 <td> 569 <ul class="nolist"> 570 <li>{@code car}: Thit b ang hin th trong gn trn -t</li> 571 <li>{@code desk}: Thit b ang hin th trong gn trn bn</li> 572 <li>{@code television}: Thit b ang hin th trn mt TV, mang n mt 573 tri nghim "10 foot" (3 mt) trong UI ca n nm trn mt mn hnh ln 574 cch xa ngi dng, c nh hng ch yu quanh DPAD hoc cch 575 tng tc khng s dng con tro khc</li> 576 <li>{@code appliance}: Thit b ang ng vai tr nh mt dng c khng 577 c mn hnh hin th</li> 578 <li>{@code watch}: Thit b c mt mn hnh hin th v c eo trn c tay</li> 579 </ul> 580 <p><em>c thm trong API mc 8, TV c thm trong API 13, ng h c thm trong API 20.</em></p> 581 <p> bit thng tin v cch ng dng ca bn hi p khi thit b c cm vo hoc 582 rt khi , hy c <a href="{@docRoot}training/monitoring-device-state/docking-monitoring.html">Xc nh 583 v Theo di Trng thi v Loi </a>.</p> 584 <p>Gi tr ny c th thay i trong sut vng i ng dng ca bn nu ngi dng t 585 thit b vo . Bn c th kch hot hoc v hiu ha mt s ch ny bng cch s dng {@link 586 android.app.UiModeManager}. Xem phn <a href="runtime-changes.html">X l Thay i Thi gian chy</a> 587 bit thng tin v nh hng ca iu ny ti ng dng ca bn trong thi gian chy.</p> 588 </td> 589 </tr> 590 <tr id="NightQualifier"> 591 <td>Ch ban m</td> 592 <td> 593 <code>night</code><br/> 594 <code>notnight</code> 595 </td> 596 <td> 597 <ul class="nolist"> 598 <li>{@code night}: Thi gian ban m</li> 599 <li>{@code notnight}: Thi gian ban ngy</li> 600 </ul> 601 <p><em>c thm trong API mc 8.</em></p> 602 <p>Gi tr ny c th thay i trong sut vng i ng dng ca bn nu ch ban m c 603 ch t ng (mc nh), trong trng hp ch s thay i da vo thi gian trong ngy. Bn c th kch hot 604 hoc v hiu ha ch ny bng cch s dng {@link android.app.UiModeManager}. Xem phn <a href="runtime-changes.html">X l Thay i Thi gian chy</a> bit thng tin v nh hng ca iu ny ti 605 ng dng ca bn trong thi gian chy.</p> 606 </td> 607 </tr> 608 <tr id="DensityQualifier"> 609 <td>Mt im nh mn hnh (dpi)</td> 610 <td> 611 <code>ldpi</code><br/> 612 <code>mdpi</code><br/> 613 <code>hdpi</code><br/> 614 <code>xhdpi</code><br/> 615 <code>xxhdpi</code><br/> 616 <code>xxxhdpi</code><br/> 617 <code>nodpi</code><br/> 618 <code>tvdpi</code> 619 </td> 620 <td> 621 <ul class="nolist"> 622 <li>{@code ldpi}: Mn hnh mt thp; xp x 120dpi.</li> 623 <li>{@code mdpi}: Mn hnh mt trung bnh (trn HVGA truyn thng); xp x 624 160dpi.</li> 625 <li>{@code hdpi}: Mn hnh mt cao; xp x 240dpi.</li> 626 <li>{@code xhdpi}: Mn hnh mt siu cao; xp x 320dpi. <em>c thm trong API 627 Mc 8</em></li> 628 <li>{@code xxhdpi}: Mn hnh mt siu siu cao; xp x 480dpi. <em>c thm trong API 629 Mc 16</em></li> 630 <li>{@code xxxhdpi}: Mt siu siu siu cao s dng (ch biu tng trnh khi chy, xem 631 <a href="{@docRoot}guide/practices/screens_support.html#xxxhdpi-note">ghi ch</a> 632 trong <em>H tr Nhiu Mn hnh</em>); xp x 640dpi. <em>c thm trong API 633 Mc 18</em></li> 634 <li>{@code nodpi}: Loi ny c th c s dng cho ti nguyn bitmap m bn khng mun c nh c 635 cho khp vi mt ca thit b.</li> 636 <li>{@code tvdpi}: Mn hnh trong khong gia mdpi v hdpi; xp x 213dpi. y 637 khng c coi l nhm mt "c bn". N c dnh ch yu cho TV v hu ht 638 cc ng dng khng cn n—vi iu kin cc ti nguyn mdpi v hpdi cho hu ht ng dng 639 v h thng s nh c chng cho ph hp. Hn nh ny c gii thiu vi API mc 13.</li> 640 </ul> 641 <p>C t l nh c 3:4:6:8:12:16 gia su mt c bn (b qua mt 642 tvdpi). V th, mt tp bimap 9x9 trong ldpi s bng 12x12 trong mdpi, 18x18 trong hdpi, 24x24 trong xhdpi, v.v. 643 </p> 644 <p>Nu bn quyt nh rng ti nguyn hnh nh ca mnh khng p trn TV hoc 645 mt s thit b khc v mun th ti nguyn tvdpi, h s nh c s bng 1,33*mdpi. V 646 d, mt hnh nh 100px x 100px i vi mn hnh mdpi s bng 133px x 133px i vi tvdpi.</p> 647 <p class="note"><strong>Lu :</strong> Vic s dng mt hn nh mt khng hm rng cc 648 ti nguyn <em>ch</em> p dng cho mn hnh c mt . Nu bn khng cung cp cho cc ti nguyn 649 thay th vi cc hn nh khp tt hn vi cu hnh thit b hin ti, h thng c th s dng 650 bt k ti nguyn no <a href="#BestMatch">ph hp nht</a>.</p> 651 <p>Xem <a href="{@docRoot}guide/practices/screens_support.html">H tr Nhiu 652 Mn hnh</a> bit thm thng tin v cch x l cc mt mn hnh khc nhau v cch Android 653 c th nh c bitmap ca mnh cho va vi mt hin ti.</p> 654 </td> 655 </tr> 656 <tr id="TouchscreenQualifier"> 657 <td>Loi mn hnh cm ng</td> 658 <td> 659 <code>notouch</code><br/> 660 <code>finger</code> 661 </td> 662 <td> 663 <ul class="nolist"> 664 <li>{@code notouch}: Thit b khng c mn hnh cm ng.</li> 665 <li>{@code finger}: Thit b c mn hnh cm ng 666 c s dng thng qua tng tc hng ca ngn tay ca ngi dng.</li> 667 </ul> 668 <p>Xem thm trng cu hnh {@link android.content.res.Configuration#touchscreen}, 669 n cho bit loi mn hnh cm ng trn thit b.</p> 670 </td> 671 </tr> 672 <tr id="KeyboardAvailQualifier"> 673 <td>S sn c ca bn phm</td> 674 <td> 675 <code>keysexposed</code><br/> 676 <code>keyshidden</code><br/> 677 <code>keyssoft</code> 678 </td> 679 <td> 680 <ul class="nolist"> 681 <li>{@code keysexposed}: Thit b c sn mt bn phm. Nu thit b c mt 682 bn phm mm c kch hot (c kh nng), gi tr ny c th c s dng khi bn phm cng 683 <em>khng</em> hin th trc ngi dng, ngay c khi thit b khng c bn phm cng. Nu khng c 684 bn phm mm hoc bn phm mm b v hiu ha, khi gi tr ny ch c s dng khi mt bn phm cng c 685 hin th.</li> 686 <li>{@code keyshidden}: Thit b c sn mt bn phm cng nhng n b 687 n i <em>v </em> thit b <em>khng</em> c bn phm mm c kch hot.</li> 688 <li>{@code keyssoft}: Thit b c mt bn phm mm c kch hot d n c 689 hin th hay khng.</li> 690 </ul> 691 <p>Nu bn cung cp cc ti nguyn <code>keysexposed</code>, nhng khng cung cp ti nguyn <code>keyssoft</code> 692 , h thng s s dng ti nguyn <code>keysexposed</code> m khng ph thuc vo vic c hin th 693 bn phm hay khng, min l h thng c kch hot mt bn phm mm.</p> 694 <p>Gi tr ny c th thay i trong vng i ng dng ca bn nu ngi dng m mt bn phm 695 cng. Xem phn <a href="runtime-changes.html">X l Thay i Thi gian chy</a> bit thng tin v 696 nh hng ca iu ny ti ng dng ca bn trong thi gian chy.</p> 697 <p>Xem thm cc trng cu hnh {@link 698 android.content.res.Configuration#hardKeyboardHidden} v {@link 699 android.content.res.Configuration#keyboardHidden}, theo tng ng cho bit mc hin th ca bn phm 700 cng v mc hin th ca bt k loi bn phm no (bao gm bn phm mm).</p> 701 </td> 702 </tr> 703 <tr id="ImeQualifier"> 704 <td>Phng php nhp liu vn bn chnh</td> 705 <td> 706 <code>nokeys</code><br/> 707 <code>qwerty</code><br/> 708 <code>12key</code> 709 </td> 710 <td> 711 <ul class="nolist"> 712 <li>{@code nokeys}: Thit b khng c phm cng cho vic nhp liu vn bn.</li> 713 <li>{@code qwerty}: Thit b c mt bn phm qwerty cng, d n c hin th vi 714 ngi dng 715 hay khng.</li> 716 <li>{@code 12key}: Thit b c mt bn phm 12-phm cng, d n c hin th vi 717 ngi dng hay khng.</li> 718 </ul> 719 <p>Xem thm trng cu hnh {@link android.content.res.Configuration#keyboard}, trong 720 cho bit phng php nhp liu vn bn chnh sn c.</p> 721 </td> 722 </tr> 723 <tr id="NavAvailQualifier"> 724 <td>S sn c ca phm iu hng</td> 725 <td> 726 <code>navexposed</code><br/> 727 <code>navhidden</code> 728 </td> 729 <td> 730 <ul class="nolist"> 731 <li>{@code navexposed}: C sn phm iu hng cho ngi dng.</li> 732 <li>{@code navhidden}: Khng c sn phm iu hng (chng hn nh pha sau mt np 733 ng).</li> 734 </ul> 735 <p>Gi tr ny c th thay i trong sut vng i ng dng ca bn nu ngi dng lm hin 736 phm iu hng. Xem phn <a href="runtime-changes.html">X l Thay i Thi gian chy</a> 737 bit thng tin v nh hng ca iu ny ti ng dng ca bn trong thi gian chy.</p> 738 <p>Xem thm trng cu hnh {@link android.content.res.Configuration#navigationHidden}, 739 c cho bit cc phm iu hng c b n khng.</p> 740 </td> 741 </tr> 742 <tr id="NavigationQualifier"> 743 <td>Phng php iu hng khng cm ng chnh</td> 744 <td> 745 <code>nonav</code><br/> 746 <code>dpad</code><br/> 747 <code>trackball</code><br/> 748 <code>wheel</code> 749 </td> 750 <td> 751 <ul class="nolist"> 752 <li>{@code nonav}: Thit b khng c phng tin iu hng ngoi cch s dng 753 mn hnh cm ng.</li> 754 <li>{@code dpad}: Thit b c bn iu hng (d-pad) iu hng.</li> 755 <li>{@code trackball}: Thit b c bi xoay iu hng.</li> 756 <li>{@code wheel}: Thit b c bnh xe iu hng iu hng (khng ph bin).</li> 757 </ul> 758 <p>Xem thm trng cu hnh {@link android.content.res.Configuration#navigation}, trong 759 cho bit loi phng php iu hng sn c.</p> 760 </td> 761 </tr> 762 <!-- DEPRECATED 763 <tr> 764 <td>Screen dimensions</td> 765 <td>Examples:<br/> 766 <code>320x240</code><br/> 767 <code>640x480</code><br/> 768 etc. 769 </td> 770 <td> 771 <p>The larger dimension must be specified first. <strong>This configuration is deprecated 772 and should not be used</strong>. Instead use "screen size," "wider/taller screens," and "screen 773 orientation" described above.</p> 774 </td> 775 </tr> 776 --> 777 <tr id="VersionQualifier"> 778 <td>Phin bn Nn tng (Mc API)</td> 779 <td>V d:<br/> 780 <code>v3</code><br/> 781 <code>v4</code><br/> 782 <code>v7</code><br/> 783 v.v.</td> 784 <td> 785 <p>Mc API c h tr bi thit b. V d, <code>v1</code> i vi API mc 786 1 (thit b phin bn Android 1.0 hoc cao hn) v <code>v4</code> i vi API mc 4 (thit b phin bn Android 787 1.6 hoc cao hn). Xem ti liu <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#ApiLevels">Mc API ca Android</a> bit thm thng tin 788 v nhng gi tr ny.</p> 789 </td> 790 </tr> 791 </table> 792 793 794 <p class="note"><strong>Lu :</strong> Mt s hn nh cu hnh c thm k t phin bn Android 795 1.0, v th khng phi tt c phin bn Android u h tr tt c hn nh. Vic s dng mt hn nh mi s hm 796 thm hn nh phin bn nn tng sao cho cc thit b c hn chc chn s b qua n. V d, s dng 797 mt hn nh <code>w600dp</code> s t ng bao gm hn nh <code>v13</code>, v 798 hn nh chiu rng kh dng mi c trong API mc 13. trnh bt k s c no, hy lun a vo mt tp hp 799 cc ti nguyn mc nh (tp hp cc ti nguyn <em>khng c hn nh</em>). bit thm thng tin, hy xem phn 800 ni v <a href="#Compatibility">Cung cp Tnh tng thch gia Thit b vi Ti nguyn 801 Tt nht</a>.</p> 802 803 804 805 <h3 id="QualifierRules">Quy tc v tn hn nh</h3> 806 807 <p>Sau y l mt s quy tc v vic s dng tn ca hn nh cu hnh:</p> 808 809 <ul> 810 <li>Bn c th quy nh nhiu hn nh cho mt tp hp n l cc ti nguyn, c tch ring bi du gch ngang. V 811 d, <code>drawable-en-rUS-land</code> s p dng cho cc thit b US-English hng 812 kh ngang.</li> 813 <li>Cc hn nh phi theo th t lit k trong <a href="#table2">bng 2</a>. V 814 d: 815 <ul> 816 <li>Sai: <code>drawable-hdpi-port/</code></li> 817 <li>ng: <code>drawable-port-hdpi/</code></li> 818 </ul> 819 </li> 820 <li>Cc th mc ti nguyn thay th khng c lng nhau. V d, bn khng c c 821 <code>res/drawable/drawable-en/</code>.</li> 822 <li>Cc gi tr khng phn bit ch hoa/thng. Trnh bin dch ti nguyn s chuyn tn th mc 823 thnh ch thng trc khi x l trnh cc vn xy ra trn h thng tp 824 khng phn bit ch hoa/thng. Bt k vic i sang ch hoa no trong tn ch nhm mc ch d c hn.</li> 825 <li>Ch h tr mt gi tr cho mi loi hn nh. V d, nu bn mun s dng 826 cng cc tp v c cho ting Ty Ban Nha v ting Php, bn <em>khng th</em> t tn th mc l 827 <code>drawable-rES-rFR/</code>. Thay vo , bn cn hai th mc ti nguyn chng hn nh 828 <code>drawable-rES/</code> v <code>drawable-rFR/</code>, trong cha cc tp ph hp. 829 Tuy nhin, bn khng bt buc thc s phi to bn sao cc tp nh nhau c hai v tr. Thay vo , bn c th 830 to mt b danh ti mt ti nguyn. Xem phn <a href="#AliasResources">To 831 ti nguyn b danh</a> bn di.</li> 832 </ul> 833 834 <p>Sau khi bn lu ti nguyn thay th vo cc th mc c t tn bng 835 nhng hn nh ny, Android s t ng p dng cc ti nguyn trong ng dng ca bn da trn 836 cu hnh thit b hin ti. C mi ln yu cu mt ti nguyn, Android li kim tra cc th mc ti nguyn 837 thay th cha tp ti nguyn c yu cu, ri <a href="#BestMatch">tm ti nguyn 838 so khp ph hp nht</a> (c trnh by bn di). Nu khng c ti nguyn thay th khp 839 vi mt cu hnh thit b c th, khi Android s s dng cc ti nguyn mc nh tng ng ( 840 tp hp cc ti nguyn cho mt loi ti nguyn c th khng bao gm hn nh 841 cu hnh).</p> 842 843 844 845 <h3 id="AliasResources">To ti nguyn b danh</h3> 846 847 <p>Khi bn c mt ti nguyn mun s dng cho nhiu hn mt cu hnh 848 thit b (nhng khng mun cung cp lm ti nguyn mc nh), bn khng cn t 849 cng ti nguyn vo nhiu hn mt th mc ti nguyn thay th. Thay vo , bn c th (trong mt s trng hp) to mt ti nguyn 850 thay th 851 ng vai tr nh mt b danh cho ti nguyn c lu trong th mc ti nguyn mc nh ca bn.</p> 852 853 <p class="note"><strong>Lu :</strong> Khng phi tt c ti nguyn u a ra c ch m theo bn c th 854 to mt b danh ti mt ti nguyn khc. C th, hot hnh, menu, ti nguyn th v cc ti nguyn 855 khng c quy nh khc trong th mc {@code xml/} khng cung cp tnh nng ny.</p> 856 857 <p>V d, hy tng tng bn c mt biu tng ng dng, {@code icon.png}, v cn phin bn duy nht ca 858 n cho cc bn a khc nhau. Tuy nhin, hai bn a English-Canadian v French-Canadian, cn 859 s dng cng phin bn. Bn c th gi s rng mnh cn sao chp cng hnh nh 860 vo th mc ti nguyn cho c English-Canadian v French-Canadian, nhng iu 861 khng ng. Thay vo , bn c th lu hnh nh c s dng cho c hai thnh {@code icon_ca.png} (bt k 862 tn no khc ngoi {@code icon.png}) v t 863 n vo th mc {@code res/drawable/} mc nh. Sau , to mt tp {@code icon.xml} trong {@code 864 res/drawable-en-rCA/} v {@code res/drawable-fr-rCA/} tham chiu ti ti nguyn {@code icon_ca.png} 865 bng cch s dng phn t {@code <bitmap>}. iu ny cho php bn lu tr ch mt phin bn ca tp 866 PNG v hai tp XML nh tr ti n. (V d v tp XML c trnh by bn di.)</p> 867 868 869 <h4>Ni dung v c</h4> 870 871 <p> to mt b danh cho mt ni dung v c ang tn ti, hy s dng phn t {@code <bitmap>}. 872 V d:</p> 873 874 <pre> 875 <?xml version="1.0" encoding="utf-8"?> 876 <bitmap xmlns:android="http://schemas.android.com/apk/res/android" 877 android:src="@drawable/icon_ca" /> 878 </pre> 879 880 <p>Nu bn lu tp ny thnh {@code icon.xml} (trong mt th mc ti nguyn thay th chng hn nh 881 {@code res/drawable-en-rCA/}), n s c bin dch vo mt ti nguyn m bn 882 c th tham chiu nh l {@code R.drawable.icon}, nhng thc t li l b danh cho ti nguyn {@code 883 R.drawable.icon_ca} (c lu trong {@code res/drawable/}).</p> 884 885 886 <h4>B tr</h4> 887 888 <p> to mt b danh cho mt b tr hin ti, hy s dng phn t {@code <include>} 889 , c bc trong mt {@code <merge>}. V d:</p> 890 891 <pre> 892 <?xml version="1.0" encoding="utf-8"?> 893 <merge> 894 <include layout="@layout/main_ltr"/> 895 </merge> 896 </pre> 897 898 <p>Nu bn lu tp ny thnh {@code main.xml}, n s c bin dch thnh mt ti nguyn m bn c th tham chiu 899 nh l {@code R.layout.main}, nhng thc t li l mt b danh cho ti nguyn {@code R.layout.main_ltr} 900 .</p> 901 902 903 <h4>Xu v cc gi tr n gin khc</h4> 904 905 <p> to mt b danh cho mt xu hin c, ch cn s dng ID ti nguyn ca xu 906 mong mun lm gi tr cho xu mi. V d:</p> 907 908 <pre> 909 <?xml version="1.0" encoding="utf-8"?> 910 <resources> 911 <string name="hello">Hello</string> 912 <string name="hi">@string/hello</string> 913 </resources> 914 </pre> 915 916 <p>Ti nguyn {@code R.string.hi} lc ny l mt b danh cho {@code R.string.hello}.</p> 917 918 <p> <a href="{@docRoot}guide/topics/resources/more-resources.html">Cc gi tr n gin khc</a> cng 919 hot ng tng t. V d, mu sc:</p> 920 921 <pre> 922 <?xml version="1.0" encoding="utf-8"?> 923 <resources> 924 <color name="yellow">#f00</color> 925 <color name="highlight">@color/red</color> 926 </resources> 927 </pre> 928 929 930 931 932 <h2 id="Compatibility">Cung cp Tnh tng thch gia Thit b vi Ti nguyn Tt nht</h2> 933 934 <p> ng dng ca bn h tr nhiu cu hnh thit b, mt iu rt quan trng l 935 bn lun cung cp cc ti nguyn mc nh cho tng loi ti nguyn m ng dng ca bn s dng.</p> 936 937 <p>V d, nu ng dng ca bn h tr vi ngn ng, hy lun bao gm mt th mc {@code 938 values/} (trong , xu ca bn c lu) <em>m khng cn</em> mt <a href="#LocaleQualifier">hn nh ngn ng v khu vc</a>. Nu thay vo bn t tt c tp xu ca mnh 939 vo cc th mc c mt hn nh ngn ng v khu vc, khi ng dng ca bn s b li khi chy 940 trn mt thit b c t mt ngn ng m cc xu ca bn khng h tr. Nhng min l bn cung cp cc ti nguyn 941 {@code values/} mc nh, khi ng dng ca bn s chy bnh thng (ngay c khi ngi dng khng 942 hiu ngn ng —vy cn tt hn l b li).</p> 943 944 <p>Tng t, nu bn cung cp cc ti nguyn b tr khc nhau da trn hng ca mn hnh, bn nn 945 chn mt hng lm mc nh ca mnh. V d, thay v cung cp ti nguyn b tr trong {@code 946 layout-land/} cho kh ngang v {@code layout-port/} cho kh dc, hy mt ci lm mc nh, chng hn nh 947 {@code layout/} i vi kh ngang v {@code layout-port/} i vi kh dc.</p> 948 949 <p>Vic cung cp ti nguyn mc nh quan trng khng ch bi ng dng ca bn c th chy trn mt 950 cu hnh m bn cha ngh n, m cn bi cc phin bn Android mi i khi thm 951 hn nh cu hnh m nhng phin bn c hn khng h tr. Nu bn s dng mt hn nh ti nguyn mi, 952 nhng vn duy tr tnh tng thch v m vi cc phin bn c hn ca Android th khi mt phin bn c hn ca 953 Android chy trn ng dng ca bn, n s b li nu bn khng cung cp ti nguyn mc nh, do n 954 khng th s dng ti nguyn c t tn bng hn nh mi. V d, nu <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code 955 minSdkVersion}</a> ca bn c t bng 4, v bn xc nh tt c ti nguyn v c ca mnh bng cch s dng <a href="#NightQualifier">ch ban m</a> ({@code night} hoc {@code notnight}, c thm trong API 956 Mc 8), khi mt thit b API mc 4 s khng th truy cp ti nguyn v c ca bn v s b li. Trong trng hp 957 ny, bn c th mun {@code notnight} lm ti nguyn mc nh ca mnh, v th bn nn loi tr hn nh 958 sao cho ti nguyn v c ca bn trong {@code drawable/} hoc {@code drawable-night/}.</p> 959 960 <p>V vy, mang li kh nng tng thch vi thit b tt nht, hy lun cung cp ti nguyn 961 mc nh cho nhng ti nguyn m ng dng ca bn cn thc hin ng cch. Sau , hy to ti nguyn 962 thay th cho cc cu hnh thit b c th bng cch s dng hn nh cu hnh.</p> 963 964 <p>C mt ngoi l i vi quy tc ny: Nu <a href="{@docRoot}guide/topics/manifest/uses-sdk-element.html#min">{@code minSdkVersion}</a> ca ng dng ca bn bng 4 hoc 965 ln hn, bn <em>khng</em> cn n ti nguyn v c mc nh khi cung cp ti nguyn 966 v c thay th bng hn nh <a href="#DensityQualifier">mt mn hnh</a>. K c khi khng c 967 ti nguyn v c mc nh, Android cng c th tm thy kt qu khp tt nht trong s cc mt mn hnh thay th v s nh c 968 bitmap nu cn. Tuy nhin, c tri nghim tt nht trn tt c thit b, bn nn 969 cung cp ni dung v c thay th cho c ba loi mt .</p> 970 971 972 973 <h2 id="BestMatch">Cch Android tm Ti nguyn Khp Tt nht</h2> 974 975 <p>Khi bn yu cu mt ti nguyn m bn cung cp ni dung thay th cho n, Android s la chn 976 ti nguyn thay th s dng vo thi gian chy, ty vo cu hnh thit b hin ti. 977 din t cch Android la chn mt ti nguyn thay th, gi s c cc th mc v c sau, 978 mi th mc li cha cc phin bn khc nhau ca cng hnh nh:</p> 979 980 <pre class="classic no-pretty-print"> 981 drawable/ 982 drawable-en/ 983 drawable-fr-rCA/ 984 drawable-en-port/ 985 drawable-en-notouch-12key/ 986 drawable-port-ldpi/ 987 drawable-port-notouch-12key/ 988 </pre> 989 990 <p>V gi s cu hnh thit b nh sau:</p> 991 992 <p style="margin-left:1em;"> 993 Bn a = <code>en-GB</code> <br/> 994 Hng mn hnh = <code>port</code> <br/> 995 Mt im nh mn hnh = <code>hdpi</code> <br/> 996 Loi mn hnh cm ng = <code>notouch</code> <br/> 997 Phng php nhp liu vn bn chnh = <code>12key</code> 998 </p> 999 1000 <p>Bng cch so snh cu hnh thit b vi cc ti nguyn thay th sn c, Android s la chn 1001 ni dung v c t {@code drawable-en-port}.</p> 1002 1003 <p>H thng ra quyt nh ca mnh v cc ti nguyn no s s dng bng l-gic 1004 sau:</p> 1005 1006 1007 <div class="figure" style="width:371px"> 1008 <img src="{@docRoot}images/resources/res-selection-flowchart.png" alt="" height="471" /> 1009 <p class="img-caption"><strong>Hnh 2.</strong> Lu v cch Android tm ti nguyn 1010 khp tt nht.</p> 1011 </div> 1012 1013 1014 <ol> 1015 <li>Loi b cc tp ti nguyn m tri vi cu hnh thit b. 1016 <p>Th mc <code>drawable-fr-rCA/</code> b loi b v n 1017 tri vi bn a <code>en-GB</code>.</p> 1018 <pre class="classic no-pretty-print"> 1019 drawable/ 1020 drawable-en/ 1021 <strike>drawable-fr-rCA/</strike> 1022 drawable-en-port/ 1023 drawable-en-notouch-12key/ 1024 drawable-port-ldpi/ 1025 drawable-port-notouch-12key/ 1026 </pre> 1027 <p class="note"><strong>Ngoi l:</strong> Mt im nh mn hnh l mt hn nh khng 1028 b loi b do tri ngc. Mc d mt mn hnh ca thit b l hdpi, 1029 <code>drawable-port-ldpi/</code> khng b loi b v mi mt mn hnh u 1030 c coi l mt kt qu khp ti thi im ny. Bn c th tham kho thm thng tin trong ti liu <a href="{@docRoot}guide/practices/screens_support.html">H tr Nhiu 1031 Mn hnh</a>.</p></li> 1032 1033 <li>Chn hn nh c mc u tin cao nht (tip theo) trong danh sch (<a href="#table2">bng 2</a>). 1034 (Bt u bng MCC, sau di chuyn xung.) </li> 1035 <li>C th mc ti nguyn no bao gm hn nh ny khng? </li> 1036 <ul> 1037 <li>Nu Khng, hy quay li bc 2 v tm vi hn nh tip theo. (Trong v d, 1038 cu tr li l "khng" ti khi i n hn nh ngn ng.)</li> 1039 <li>Nu C, tip tc sang bc 4.</li> 1040 </ul> 1041 </li> 1042 1043 <li>Loi b cc th mc ti nguyn khng bao gm hn nh ny. Trong v d, h thng 1044 s loi b tt c th mc khng bao gm hn nh ngn ng:</li> 1045 <pre class="classic no-pretty-print"> 1046 <strike>drawable/</strike> 1047 drawable-en/ 1048 drawable-en-port/ 1049 drawable-en-notouch-12key/ 1050 <strike>drawable-port-ldpi/</strike> 1051 <strike>drawable-port-notouch-12key/</strike> 1052 </pre> 1053 <p class="note"><strong>Ngoi l:</strong> Nu hn nh ang xt l mt im nh mn hnh, 1054 Android s chn ty chn khp gn nht vi mt mn hnh ca thit b. 1055 Nhn chung, Android u tin gim kch c mt hnh nh ban u ln hn thay v tng kch c mt hnh nh ban u 1056 nh hn. Xem phn <a href="{@docRoot}guide/practices/screens_support.html">H tr Nhiu 1057 Mn hnh</a>.</p> 1058 </li> 1059 1060 <li>Quay li v lp li cc bc 2, 3 v 4 ti khi ch cn li mt th mc. Trong v d, hng 1061 mn hnh l hn nh tip theo nu c kt qu khp. 1062 V th, cc ti nguyn khng quy nh hng mn hnh s b loi b: 1063 <pre class="classic no-pretty-print"> 1064 <strike>drawable-en/</strike> 1065 drawable-en-port/ 1066 <strike>drawable-en-notouch-12key/</strike> 1067 </pre> 1068 <p>Th mc cn li l {@code drawable-en-port}.</p> 1069 </li> 1070 </ol> 1071 1072 <p>Mc d quy trnh ny c thc thi cho tng ti nguyn c yu cu, h thng s ti u ha hn na 1073 mt s kha cnh. Mt cch ti u ha nh vy l sau khi bit cu hnh thit b, n c th 1074 loi b cc ti nguyn thay th m khng th khp c. V d, nu ngn ng cu hnh 1075 l English ("en"), khi bt k th mc ti nguyn no c hn nh ngn ng c t thnh 1076 ngn ng khc English u s khng c bao gm trong tp hp cc ti nguyn c kim tra (mc d 1077 th mc ti nguyn <em>khng c</em> hn nh ngn ng vn c bao gm).</p> 1078 1079 <p>Khi la chn ti nguyn da trn hn nh kch c mn hnh, h thng s s dng cc ti nguyn 1080 c thit k cho mn hnh nh hn mn hnh hin ti nu khng c ti nguyn no khp tt hn 1081 (v d, mt mn hnh kch c ln s s dng cc ti nguyn mn hnh kch c bnh thng nu cn). Tuy nhin, nu 1082 nhng ti nguyn duy nht sn c li <em>ln hn</em> mn hnh hin ti, h thng s 1083 <strong>khng</strong> s dng chng v ng dng ca bn s b li nu khng c ti nguyn no khc khp vi cu hnh 1084 thit b (v d, nu tt c ti nguyn b tr u c gn th bng hn nh {@code xlarge}, 1085 nhng thit b li c mt mn hnh kch c bnh thng).</p> 1086 1087 <p class="note"><strong>Lu :</strong> <em>Mc u tin</em> ca hn nh (trong <a href="#table2">bng 2</a>) quan trng 1088 hn s lng hn nh khp chnh xc vi thit b. V d, trong bc 4 bn trn 1089 la chn trn danh sch bao gm ba hn nh khp chnh xc vi thit b (hng, loi 1090 mn hnh cm ng, v phng php nhp liu), trong khi <code>drawable-en</code> ch c mt tham s khp 1091 (ngn ng). Tuy nhin, ngn ng c mc u tin cao hn c ba hn nh khc ny, v th 1092 <code>drawable-port-notouch-12key</code> b loi.</p> 1093 1094 <p> tm hiu thm v cch s dng ti nguyn trong ng dng ca bn, hy tip tc sang phn <a href="accessing-resources.html">Truy cp Ti nguyn</a>.</p> 1095